I would like to have the option to use taxonomy in the url instead of only having category like "uncategorized." Note that I have multiple taxonomies (countries, states etc), some of which have thousands of terms (if that even matters).

Edit: I failed to mention that this is an offshoot of another question: Pros and cons of using [taxonomy name] in place of [category name]?

The goal is a permalink that looks something like domain.com/2011/taxonomy-term/post-name.

So if I have a taxonomy named countries and a post about Egypt, I want to be able to plug the Egypt term into that url.

  • You mention that you have multiple taxonomies. Do you want to have them all in the URL? Please provide a few example URLs.
    – scribu
    Commented Feb 15, 2011 at 1:10
  • updated the post
    – Adam
    Commented Feb 16, 2011 at 20:07
  • How you do decide which taxonomy to use in the URL? This can change from post to post?
    – Jan Fabry
    Commented Feb 17, 2011 at 13:53
  • @Jan: Maybe we don't understand each other but yes, I need to be able to change the taxonomy for each post. It would be based on the taxonomy I check on the right panel just like the way categories works now as is. If I check multiple categories, I think the one in URL is based on higher ID or alphabetical order.
    – Adam
    Commented Feb 18, 2011 at 0:08

2 Answers 2


Changing the URL structure of a post always consists of two parts: writing the new URL in your links, and handling the new structure.

Handling the new structure

Handling the new structure is easy: because the post name (slug) is enough to identify a post, we can ignore the taxonomy you are using in the URL. In theory you should be able to use any taxonomy, but in practice you can't without some extra tweaks.

If you use %category%, the redirect_canonical() function kicks in and tries to redirect all requests with a term from another taxonomy in the URL to one with the category (because using only one URL for each post will prevent spreading page rank for the same content). You see this when all your URLs seem to go to /uncategorized/[post-name]/. You can unhook the function entirely, or hook into it and prevent the redirect if it's not needed.

The other option would be to use one of your custom taxonomies in the permalink structure. This will prevent redirect_canonical() from doing anything (until that function is improved to deal with custom taxonomies too), but because of an oversight in the WP 3.0 version of custom taxonomies, it won't generate hierarchical rewrite rules (this will be fixed in 3.1).

What I tried instead, and what seems to work, is to create a "dummy" rewrite structure, called %anything%, which matches .+? (anything, but not greedy). In this case you should also re-enable the verbose page rules, because WordPress doesn't notice that something is going on.

add_action('init', 'wpse9346_init');
function wpse9346_init()
    $GLOBALS['wp_rewrite']->add_rewrite_tag( '%anything%', '(.+?)', 'wpse9346_dummy=' );
    $GLOBALS['wp_rewrite']->use_verbose_page_rules = true;

The wpse9346_dummy parameter is ignored, so it should be rather safe to use this. My rewrite structure now is /%anything%/%postname%/.

Writing URLs of the new structure

This is the part where you have to do some thinking: if a post can be tagged with terms from different taxonomies, what taxonomy should we use to create a link to the post? Say we have one taxonomy for countries, and one for government offices. A post about Mubarak would be tagged with Egypt in one taxonomy and President in another. Should the post URL be /egypt/mubarak-steps-down/ or /president/mubarak-steps-down/? Both URLs will work and end up at the same post, but if you use my %anything% solution from above the permalink will have %anything% in it, which is of course invalid. So hook into post_link and modify the URL the way you need it to be.


I did something similar. This article showed me the way:


  • Thanks, I'm going to take a closer look at this link on Friday.
    – Adam
    Commented Feb 18, 2011 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.