Pretty urls with multiple "optional" custom taxonomies
-
I use 4 custom taxonomies (city, place, activity, region, all of them non-hierarchical). Based on these four taxonomies, one category (country) and one tag(type of trip), I display posts that match terms for these taxonomies – not necessarily all 6 taxonomies must be provided – e.g. if only category, city and tag are provided, posts that match these 3 taxonomies are displayed.
When I approach these posts via parameters in URL (e.g. https://example.com/?category_name=USA®ion=hawai&place=oahu&city=honolulu&activity=surfing&tag=sightseeing), everything is working, because I can easily omit any of taxonomies (e.g. https://example.com/?category_name=USA®ion=hawai&tag=sightseeing).
I also managed to create rewrite rule for pretty url with all 6 taxonomies that is working properly:
'category/(.+?)/tag/(.+?)/region/(.+?)/city/(.+?)/place/(.+?)/activity/(.+?)/?$' => 'index.php?category_name=' . $wp_rewrite->preg_index(1) . '&tag=' . $wp_rewrite->preg_index(2) . '®ion=' . $wp_rewrite->preg_index(3) . '&city=' . $wp_rewrite->preg_index(4) . '&place=' . $wp_rewrite->preg_index(5) . '&activity=surfing' . $wp_rewrite->preg_index(6)
My problem is how to create rewrite rules for cases when less than all 6 taxonomies are used – I believe there must be a better solution than listing all possible combination of rewrite rules, right?
Could you please give me an advice what is the right design/solution of this problem? Can be used add_rewrite_tag() function?
Thank you!
- The topic ‘Pretty urls with multiple "optional" custom taxonomies’ is closed to new replies.