1

I have a post type called "shop" and added query variables and a rewrite rule.

// Adds query vars for shop post type
function gtp_add_shop_query_vars( $vars ) {

    $vars[] = 'shop-category';
    $vars[] = 'width';
    $vars[] = 'height';
    return $vars;       
}
add_filter( 'query_vars', 'gtp_add_shop_query_vars' );

// Adds rewrite rules for shop post type
function gtp_add_shop_rewrite_rules() {
    add_rewrite_rule( '^shop/([^/]*)/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&width=$matches[2]&height=$matches[3]', 'top' );
}
add_action('init', 'gtp_add_shop_rewrite_rules', 10, 0);

With this rules I can visit now:

https://www.example.com/shop/product/80cm/100cm/

But if I want to visit:

the regex does not match anymore and the page will not be found.

What do I need to do to be able to enter all the levels of the url?

1 Answer 1

1

I fixed my problem as follows:

// Adds rewrite rules for shop post type
function gtp_add_shop_rewrite_rules() {
    add_rewrite_rule( '^shop/([^/]*)/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&width=$matches[2]&height=$matches[3]', 'top' );
    add_rewrite_rule( '^shop/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&width=$matches[2]', 'top' );
    add_rewrite_rule( '^shop/([^/]*)$', 'index.php?shop-category=$matches[1]', 'top' );
}
add_action('init', 'gtp_add_shop_rewrite_rules', 10, 0);

Your Answer

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

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