If you look at the default WordPress archive for a taxonomy (like "Categories"), it can be hard to tell which post type the taxonomy belongs to. Fortunately, taxonomy rewrite rules help us dynamically rewrite the URL.
Ok, this is just a quick hit, but it touches on something that has annoyed me for a long time. Here’s the situation:
- Say you’ve got a Custom Post Type, like “Work.” Hypothetically.
- Say you’ve got a Custom Taxonomy for Work, called Type.
You’d expect that the standard link for an individual term of the Type Taxonomy to be “yoursite.com/work/type/term,” but it’s not. It’s “yoursite.com/type/term.”
This makes No Sense to me, because when you register the taxonomy, you tell it which Post Type it belongs to, right here:
register_taxonomy('type', 'work', array(...
That’s okay though, because we can use WordPress’ rewrite rules to change the link:
So all I’m doing here is rewriting the slug for the Type Taxonomy to “yoursite.com/work/type/” and making sure that the Permalink structure doesn’t interfere.
So how do we fix it? For the longest time, my answer was to gnash my teeth and go back to the other URL structure. However, as one of my colleagues pointed out, it’s all about the order that you register taxonomies and post types.
This may seem counter-intuitive, but you need to register the Custom Taxonomy BEFORE the Custom Post Type that it belongs to in order for the rewrite rule to work. The taxonomy needs to “exist” before the post type in order for WordPress to build the URL correctly.