Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Michael Rodriguez
Michael Rodriguez
15,211 Points

URL Rewrite for custom permalinks to show Custom Post Type Relationship in WordPress

I'm creating a WordPress site that has 3 custom post types registered: course, chapter, and lesson.

All of these post types are inter-connected and share a relationship using custom fields. For example, the chapter has a custom field that stores the course post object in which it belongs to. Same goes for the lesson and the chapter it belongs to.

My current permalink settings are using the: %category%/%postname%/ structure, in which display the post's URL like so:

www.domainname.com/custom-post-type-name/post-name

What i'm trying to accomplish is to create a custom permalink structure that shows the relationship between these 3 post types in the URL. Example below:

When user is on course: www.domainname.com/course

When user is on a chapter: www.domainname.com/course/chapter

When a user is on a lesson: www.domainname.com/course/chapter/lesson

While looking in the WordPress Codex, I found the the Rewrite API and WP_Rewrite(), I'm a little lost and i do not know where to begin.

Any help on this would be greatly appreciated.

1 Answer

Cristian Cisneros
Cristian Cisneros
794 Points

Did you figure this out?

I've worked on a project recently that followed a similar structure and I was able to complete the project using the plugin "CPT UI" plugin and by changing the "Custom Rewrite Slug".

Michael Rodriguez
Michael Rodriguez
15,211 Points

I have not figured this out yet. CPT UI is a great plugin for creating custom post types and custom taxonomies with folks that aren't experienced with code. What I'm trying to accomplish here is something a bit more dynamic. Each Post type slug will be dynamically populated by it's parent post type.

/Course

/Course --> Chapter

/Course --> Chapter --> Lesson

If you have any idea how i can accomplish this with CPT UI, please let me know. Greatly appreciated.