Permalinks for Hierarchical Custom Post Types
I wrote a WordPress plugin that lets you have a hierarchy of custom post types where the post types are different. This is the plugin here. I see a few people marked the plugin is broken, it works, but its impossible to figure out how without instructions. I don’t know why the descriptions and stuff I added don’t show up on the WordPress site, so I’ll provide instructions and support on this blog.
Its worth pointing out that I made this other developers. If you’re looking for a plug and play solution, this isn’t quite it. But if you’re a developer with the same problem, I’ve done the heavy lifting for you.
How to use:
First make sure you’re not using the default query type (?post=24&blah=foo) style of permalinks.
Suppose you have two custom post types: “book” and “author”. For posts of type “book”, you have: book1. For posts of type “author”, you have: author1.
- Go to Settings > Custom Permalinks
- Enter “library” for your slug
- Drag the “author” block from “Custom Post Types” to “Permalink Structure”
- Drag the “book” block from “Custom Post Types” to “Permalink Structure”
- Click “Add New Permalink”
Now you can go to example.com/library/author1/ to read the post named author1 (assuming that the slug for the post author1 is in fact “author1″). Suppose that book1 is a child post of author1 with a similar slug, go to example.com/library/author1/book1/ to read about book1.
You’re probably wondering how you make book1 a child post of author1. In WordPress, you can’t set the parent of a post to one of a different type through the usual methods. So how to do it? That’s up to you to figure out. You can set it manually in the database, but that’s boring. I’d personally make a meta box for the custom post type that serves as the child post (in this case, “book”). In the meta box, create a drop down menu of posts of the type of the parent (in this case, “author”). Reference the functions add_meta_box and wp_dropdown_pages for help doing this.
You might wonder why I don’t do this automatically. I also don’t automatically change the permalink for the posts. Calling the function get_permalink on book1 will just give you example.com/book1/ not our custom permalink. The reason is that you can create as many structures as you like. Use the slug “american” and create a link like example.com/american/MarkTwain/. Whatever you like.
Comment if you need help or have suggestions.