Rega Cahya Gumilang
Rega Cahya Gumilang

Reputation: 11

A single rewrite rule for multiple pages in WooCommerce

I have to created custom rewrite rule in my class.

I've made 1 rewrite rule but for multiple different pages.

add_rewrite_rule('^car/([^/]*)/([^/]*)/?', 'index.php?&product_cat_temp=$matches[1]&brand_or_product=$matches[2]', 'top');

URL example for brand: project.local/car/sport/BMW/ BMW is brand

URL example for product: project.local/mobil/sport/bmw-i8/ bmw-i8 is product name

so, when query_var brand_or_product is available, I will detect it first, is it belonging to product or brand

if the result is product, I will set the query_var product = bmw-i8 and set the query var post_type = product

if the result is brand, I will set the query_var product_cat = sport and set the query_var brand = BMW

After that, I will direct the link to each page: product page or shop page

Is this way possible?

Upvotes: 0

Views: 319

Answers (1)

TBI
TBI

Reputation: 2809

It will work, just need little change. Use :

add_rewrite_rule('^car/([^/]*)/([^/]*)/?', 'index.php?product_cat_temp=$matches[1]&brand_or_product=$matches[2]', 'top');

Upvotes: 1

Related Questions