I have two custom post types:
- Events
- Businesses
I have a new taxonomy that both these CTP’s share:
- Locations
If I go to “domain.com/events/” I see an archive of all the events, and if I go to “domain.com/businesses/” I see an archive of all the businesses. So far, so good.
If I go to “domain.com/locations/” I see an archive of all the events AND businesses, which is Ok.
However, how do I view the event locations only? “domain.com/locations/events” or “domain.com/events/locations” doesn’t work.
Do I have to change the rewrite settings for either the locations taxonomy or the CPT? It seems I’m stuck!
You’ll have to add some new rewrite rules, thankfully WordPress makes it fairly straightforward using a few function calls.
So to explain what I’m doing above:
add_rewrite_tag()
so that when you put the custom post type name into the URL (eg. /events/location/uk/) it will be recognised by the regular expression and the resulting query WP runs would containpost_type=events
/event/location/uk
will show uk events and/business/location/uk
will show uk businessesadd_permastruct()
so that WP generates the new rewrite rules whenever permalinks are refreshed. In the 3rd parameter it’s very important to havewalk_dirs
set to false as you’ll get some undesirable aggressive rewrite rules with archives for/%posttype%/category/
and/%posttype%/
Gotchas
the_terms()
to output location links for a business or event that only shows the originating post type you’ll have to filter the URLs or write a custom function to output URLs to match your permastructYou can specify the slug to use in the args when calling the register_taxonomy function by using ‘rewrite’.
So, you’d have something like the following:
And then replicate for Businesses :
Hope that helps?!
Thanks
Rik