I’m using the Custom Post Type UI plugin to create my custom taxonomies. I have a portfolio that is made up of projects (custom post type) with 2 custom taxonomies of technologies and clients. The clients taxonomy has a custom rewrite slug of portfolio/clients/
, while the technologies taxonomy has a custom rewrite slug of portfolio/
Rewrites:
portfolio/html
<- page displays all projects using HTML
portfolio/clients/client-a
<- page displays all projects for client A
Now when I try to make a landing page for clients that has a url slug of /portfolio/clients
I get the 404 page. I’m pretty sure this is because of conflicting url rewrites of the technologies taxonomy. I’m guessing as it searches for it in the technology taxonomy, it doesn’t exist then it spits out the 404 page. So how do I get the url rewrite slug to work so that when I hit /portfolio/clients
, it doesn’t send back the 404 page and it uses the correct page template?
You seem to need “partial verbose rewrite rules”. Verbose rewrite rules means all the pages are put on top because WordPress can’t figure out the difference between a page and a post. Here it thinks it can, because all URLs of the form
portfolio/([^/]+)/
are from yourportfolio
taxonomy, except this oneportfolio/clients/
. You will have to put that one on top of the rewrite rules, so it matches before the more generic portfolio taxonomy. You could probably also force all of the rewrite rules to be verbose, but that will impact the performance if you have lots of pages.This answer is written with my just-gained understanding of rewrite rules, so I hope it is a good way to do it and the example code doesn’t contain too many errors.
A page does not generate just one rewrite rule, it generates a group:
(pagename)/trackback/?$
(pagename)/feed/(feed|rdf|rss|rss2|atom)/?$
(pagename)/(feed|rdf|rss|rss2|atom)/?$
(pagename)/page/?([0-9]{1,})/?$
(pagename)/comment-page-([0-9]{1,})/?$
(pagename)(/[0-9]+)?/?$
You don’t have to create these yourself, you can re-use the power of
WP_Rewrite
. Look at itspage_rewrite_rules()
method: if we are in verbose mode, it gets a list of all pages (viapage_uri_index()
) and their attachments, overwrites the%pagename%
rewrite tag, and generates the rewrite rules for this page. We can do this too:This will give us the rules for the pages, but not yet for attachments used in the page. If you also want them, you repeat the step for each attachment, but with
add_rewrite_tag('%pagename%', "({$attachment_uri})", 'attachment=')
(seepage_rewrite_rules()
for more details).Good, we got the rules, but now you need to add them to the complete rewrite structure in some way. You could do this with
add_rewrite_rule()
, but you must call it for every rule generated in the$page_rewrite_rules
array. For this reason, many people hook into therewrite_rules_array
filter, since you can just modify the array there.After you included this filter, you should flush the rewrite rules (once, not one every page load, as it is quite heavy). You can do this by calling
flush_rewrite_rules()
, or by visiting the “Permalinks” settings page.