Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've set up a custom hierarchical taxonomy called 'region'.

So now I have e.g. url.com/region/leeds/ etc.

However, I'm now expanding to other countries, so I'm making the region sub-categories of the parent.

I want to rewrite the parent category URL to lose the /region and instead become

url.com/region/uk -> url.com/uk.

Can I use WP_Rewrite and hook into category_rewrite_rules and check if the taxonomy is a parent and then supply the edited URL?

share|improve this question
So you want parent term urls to drop the taxonomy, and the rest of the urls will be same? For example parent url will be: url.com/uk and child url will be: url.com/region/leeds/ –  Hameedullah Khan Jan 2 '12 at 13:09
yes. Exactly that. –  deadlyhifi Jan 2 '12 at 14:02
add comment

1 Answer

up vote 1 down vote accepted

This is possible, but, the effort required is not trivial, and it is not performant, I would advise against doing it.

Your regions taxonomy will have a rewrite rule similar to this:

regions/(.+?)/?$ -> region: (.+?)

What you're wanting is something similar to this:

(.+?)/?$ -> region(.+?)

The problem here is that this rule doesn't catch just example.com/uk it also catches example.com/leeds example.com/about-page example.com/feed/ etc it's too verbose.

So, there is no generic method of doing it. This means additional rules, and 2 choices, both of which will slow down the process of resolving URLs:

1: Define them all manually

This is by far the fastest method. The trade off here is time.


uk -> region: uk
fr -> region: fr

Or in code:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        'uk'    =>  'index.php?region=uk',
        'fr'    =>  'index.php?region=fr',
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );

You will need to flush permalinks when this is added/edited/removed for the URLs to work.

2: Do the above but dynamically

The problem here is that this makes the rule generation more expensive, and it gets more and more expensive as the number of terms rises. It also means you need to add a hook to the add/edit/delete hooks of that taxonomys terms so that you can flush the permalinks so they can be regenerated.

You would need to grab all the terms of the region taxonomy and loop through them, adding an appropriate entry to the array before adding to the rewrite rules.

What about the child terms?

You can either define them manually, or add a check on the loop to rewrite to regions/termnamegoeshere. You could ommit this step but WordPress may automatically redirect the browser to a longer URL name e.g. region/uk/leeds, which may or may not be to your liking.

More details on rewrite rules

You can find an answer I gave to another question with lots of links here:

Pretty URL with add_query_var

Also I recommend the Monkeyman rewrite rules for testing things during development to see exactly which rule a URL is picking up and what variables go where. You can find a link to that plugin in the answer linked above

share|improve this answer
Thank you. I'm defining them all manually as I'm in control of when they get created. You say "This is by far the fastest method. The trade off here is time". Do you mean the fastest method for WP to deal with, and it's my time that is the trade-off - or the other way round? Anyway, great answer - I think I'm starting to get my head around url rewrites now. –  deadlyhifi Mar 1 '12 at 15:20
Its the actual entry and setup of the system by a person (yourself), once its set up you should have rewrites that 'just work', regenerating rewrite rules should take only a fraction longer. –  Tom J Nowell Mar 1 '12 at 17:31
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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