Rewrite Rules for Multiple (more than 2) Taxonomies
I am using woocommerce and have some 'product attributes' which are just a taxonomies.
I have the following taxonomies:
- pa_color
- pa_material
- pa_style
the pa_ stands for product attribute and it is built-in to WooCommerce handles these taxonomies, so I can't change that.
WooCommerce also comes with product categories (product_cat) and product tags (product_tag) taxonomies.
the following query gets me all the Red, Vinyl products in the Fabrics product category
?product_cat=fabricspa_material=vinylpa_color=red
I'd like to turn this into a pretty permalink, something like:
product-category/fabrics/material/vinyl/color/red
This answer gets me pretty close to what I am trying to achieve:
URL rewrite rules for multiple taxonomies query
in that I can get my URL to handle 2 taxonomies (product_cat + 1 attribute taxonomy), but I am stuck on how to get 3 (or more) parameters.
So I can get:
/products-category/fabrics/color/red
or
/product-category/fabrics/material/vinyl
but not all 3 together
/product-category/fabrics/material/vinyl/color/red
edit / though i think it'd be ideal to have the URL be
/fabrics/vinyl/red
i'm not sure that is possible?
end edit/
WooCommerce also has some settings for modifying the permalinks (like prepending the URL with 'shop' or changing the product-category slug) so i'll need to account for that eventually, but right now I need to get this first step down.
for simplicity's sake, this is a stripped down version of my code. I'm actually getting the taxonomies from built-in woocommerce functions, but the question isn't limited to woocommerce... as it pertains to any site w/ a lot of taxonomies.
add_action( 'init', 'wpa_init' );
function wpa_init() {
$taxonomies = array(
'pa_color' = 'Color',
'pa_material' = 'Material',
'pa_style' = 'Style',
);
$base = 'product-category';
foreach ( $taxonomies as $taxonomy = $mask ):
$attribute = strtolower( sanitize_title( $mask ) );
add_rewrite_rule(
$base.'/(.+?)/' . $attribute . '/([^/]+)(/page/(\d+))?/?$',
'index.php?product_cat=$matches[1]' . $taxonomy . '=$matches[2]paged=$matches[4]',
'top'
);
endforeach;
}
this was the best resource i've found on taxonomy query permalinks: http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/
but it, too, seems stuck at 2 taxonomies.
Topic rewrite-tag rewrite-rules url-rewriting Wordpress
Category Web