3

I'm trying to create some pretty URLs for links querying multiple taxonomies. I'm using the "Taxonomy drill-down" plugin, although the main usage is now integrated @ the core it still has some helpful function for templating :)

An example URL querying multiple taxonomies ( working ) would be: domain.com/?tax1=foo&tax2=bar

My goal is to have links like - domain.com/tax1/foo/tax2/bar - translating to the above example

tax1/2 = taxonomy name

foo/bar = required term

I stumbled upon Examples of WP Rewrites but i think i suck with those patterns and something like:

$newrules['tax1/(.*)/tax2/(.*)/'] = 'index.php?tax1=$matches[1]&tax2=$matches[2]';

doesn't seem to work ( that's just a portion of the whole function based on the Wordpress example )

Any ideas? :)

1 Answer 1

5

(.*) matches everything, so it will "eat up" the extra characters (it is "greedy"). If the URL is /tax1/foo/tax2/bar/, the first (.*) will be foo/tax2/bar/, so nothing is left for the second match.

Instead of (.*) you can use the "non-greedy" version (.+?). This will match as much as possible, but still keep the rest in mind. You can also use the even stricter version ([^/]+): this will match everything up to the next / - but this will not work for nested categories, so /category/fruit/banana/tag/flies/ will not split up in fruit/banana and flies like you might expect.

If you are going to play with the rewrite rules I recommend you to install my Rewrite analyzer plugin. It allows you to see the current rewrite rules and play with URLs to see which rules will match.

7
  • Thanks for the answer Jan, i did try tweaking it up and based on Rewrite analyzer ( awesome plugin ) it should work, but i keep getting a 404 error. It must be an issue from the drill-down plugin side.
    – netfreak
    May 19, 2011 at 15:56
  • @netfreak: add_rewrite_rule( 'category/(.+?)/tag/([^/]+)/?', 'index.php?category_name=$matches[1]&tag=$matches[2]', 'top' ); works for me. The Taxonomy drill-down widget does not generate links in that format, but if I go to a URL like /category/banana/tag/hello/ it does only display posts in the category banana and with tag hello.
    – Jan Fabry
    May 19, 2011 at 16:13
  • You rock. i used your latest snippet and now the rewrite works, possible the "/?" at the end? ( i need to study on RegExp asap :) )
    – netfreak
    May 19, 2011 at 16:58
  • @netfreak: Er, I see I missed a final $ too, so it might match too much. category/(.+?)/tag/([^/]+)/?$ should be a better match - and it could be even improved to allow paging. Then it would become add_rewrite_rule( 'category/(.+?)/tag/([^/]+)(/page/(\d+))?/?$', 'index.php?category_name=$matches[1]&tag=$matches[2]&paged=$matches[4]', 'top' );
    – Jan Fabry
    May 19, 2011 at 17:01
  • 1
    @JanFabry you can use qmt_url filter for Taxonomy drill-down URLs: github.com/scribu/wp-query-multiple-taxonomies/wiki/… Nov 3, 2011 at 13:36

Your Answer

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

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