Colin Robinson

Just another guy on the internet

Permalinks for Hierarchical Custom Post Types

June 2013 update:

This plugin is not actively developed. If you want to use this plugin with the latest version of WordPress, you should reference the source code on github and hopefully that will help you develop your own solution. I’m also open to pull requests.
End of update

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.

  1. Go to Settings > Custom Permalinks
  2. Enter “library” for your slug
  3. Drag the “author” block from “Custom Post Types” to “Permalink Structure”
  4. Drag the “book” block from “Custom Post Types” to “Permalink Structure”
  5. 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.

4 Responses to “Permalinks for Hierarchical Custom Post Types”

  • James Tryon says:

    Thanks for this great little plugin.

    I had to modify line 64 of options.php from /[^a-zA-Z0-9]/ to /[^a-zA-Z0-9_\-]/ to allow dashes and underscores due to my custom post type also using dashes in its slug.

    Any chance of adding or modifying this? or maybe adding a settings page that lets you set what opts to allow?

    Anyways great plugin and hate to fork it for such a small edit.

    FYI – i used http://janina.tumblr.com/post/3588081423/post-parent-different-type to add my parent dropdown list.

    Thanks again.
    – James Tryon

  • dave C says:

    Doesn’t seem to be able to drag come WP 3.4 – :(

  • Marijn says:

    You write ” 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.”

    This is something that i would like to happen, but i can’t really figure out how to get that. Could you point me in the right direction?

    Thanks

  • R says:

    Thanks for the plugin, this works fine on our english site. However, we do have a problem on making this work with the arabic site. Any suggestion? Btw, we are using WPML to translate the contents and this is our permalinks:

    [en] /photogallery/album/photo/

    [ar] /ar/photogallery/album/photo/ (we don’t know how to make this work)

    Thanks again and really hope to have a reply from you soon.

Leave a Reply

Your email address will not be published.