Update 2010-10-04: See Understanding Rewrite Tags for a somewhat more helpful treatment of the subject.
I’ve been playing around with WordPress 3.0 to create a bug/issue-tracking plugin (i.e., using WP to track bugs, not to track WP bugs (although it could conceivably do that)). More details about the plugin will be forthcoming, but now is as good a time as any to share the solution to one problem I came across.
Creating New Post Types and Taxonomies
When you create a new post type, you use the
register_post_type function. When you create a new post type through that function, WP creates a new rewrite rule base on the name you give the post type. In my case, I’ve created an
issue post type, so all issues, by default, will have the permalink
%issue% is replaced with the post slug). Using the
rewrite argument to
register_post_type, you can change the beginning of the permalink to something different (e.g.,
/bugs/%issue%/) should you so desire.
Here’s the full function call I’m using (I’m not guaranteeing that everything here is correct or necessary, but it seems to be working):
register_post_type( 'issue', array( 'label' => 'Issues', 'singular_label' => 'Issue', 'description' => 'A bug to fix, task to complete, or anything else that need to be done', 'public' => TRUE, 'publicly_queryable' => TRUE, 'show_ui' => TRUE, 'query_var' => TRUE, 'rewrite' => TRUE, 'capability_type' => 'post', 'hierarchical' => FALSE, 'menu_position' => NULL, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'revisions'), 'menu_position' => 5, 'rewrite' => array( 'slug' => 'issue', 'with_front' => FALSE, ), ));
Similarly, you can create a custom taxonomy. In this case, I’ve created a taxonomy for projects, so issues can be assigned to projects. Much like a normal category will have a URL like
issue_project taxonomy has URLs matching
/issue_project/%issue_project%/, which gives you a page with all issues matching the given project name. You can, once again, use the
rewrite argument to give a different path (in my case, I’ve used
Here’s the function call for the taxonomy (same caveats as above):
register_taxonomy('issue_project', 'issue', array( 'label' => 'Projects', 'singular_label' => 'Project', 'public' => TRUE, 'show_tagcloud' => FALSE, 'hierarchical' => TRUE, 'query_var' => TRUE, 'rewrite' => array( 'slug' => 'project' ), ));
Putting Things Together
Now, what if you want to combine these, to show the hierarchy in your URLs (e.g.,
I first attempted to change the
slug of the
issue post type to
project/%issue_project%/issue. That didn’t work. I had URLs that looked like
%issue_project% wasn’t being replaced. So I scrapped that idea.
I dug around for a while and found the
post_type_link filter. Using that, I could add the project URL to the front of the issue URL when WP was building the permalink for an issue. That gave me the correct URLs, but they would 404.
So I dug around some more through the
WP_Rewrite class and realized that I had been wasting a lot of time when I already had the solution: do both.
- Change the
- Hook into the
post_type_linkfilter with a callback that replaces
%issue_project%with the appropriate project slug.