Conditional URL rewrite for custom post type and taxonomy
-
We have a product register where we have products as a custom post type. Connected to this we have a custom taxonomy (product_cat). We have setup rewrites so that our permalink structure looks like this:
/product/tax-1/tax-2/our-product
For this, we used this in functions.php:
function product_rewrite_rules($rules) { $newRules = array(); $newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?product=$matches[3]'; $newRules['product/(.+)/(.+)/?$'] = 'index.php?product_cat=$matches[2]'; $newRules['product/(.+)/?$'] = 'index.php?product_cat=$matches[1]'; return array_merge($newRules, $rules); } add_filter('rewrite_rules_array', 'product_rewrite_rules');
This has worked well, but for some taxonomies we need to add a third level, so permalink would look like this:
/product/tax-1/tax-2/tax-3/our-product
If we change our rewrite rules to:
function product_rewrite_rules($rules) { $newRules = array(); $newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?product=$matches[4]'; $newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?product_cat=$matches[3]'; $newRules['product/(.+)/(.+)/?$'] = 'index.php?product_cat=$matches[2]'; $newRules['product/(.+)/?$'] = 'index.php?product_cat=$matches[1]'; return array_merge($newRules, $rules); } add_filter('rewrite_rules_array', 'product_rewrite_rules');
it works for three levels of taxonomies, but not two. We can’t figure out how to get this working. I we add a conditional like:
if ( is_tax(220) ) { $newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?product=$matches[4]'; $newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?product_cat=$matches[3]'; } else { $newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?product=$matches[3]'; }
to try and run some rewrites for certain taxonomy pages and the old setup for others, it doesn’t seem to work. I can’t imagine that this is not possible to achieve, but it seems like we’re going about it the wrong way.
- The topic ‘Conditional URL rewrite for custom post type and taxonomy’ is closed to new replies.