I have a custom post type called shop with product posts in it. This post type is connected to a taxonomy shop-category, to specify a product section inside the shop.

Each product has a item_width and item_height custom fields. And as you can see below I made some urls to filter the products by width and height:

https://www.example.com/shop-category/product/
https://www.example.com/shop-category/product/80/
https://www.example.com/shop-category/product/80/200/
https://www.example.com/shop-category/product/80/200/page/2/

The last two levels of the last url (/page/2/) are set when you use the pagination to navigate to the next page with products. At the moment you can only visit this page when the item_width and item_height query parameters are set. Now I am questioning if it possible to make this also work when the item_width and item_height are not set.

So for example with the following url:

https://www.example.com/shop-category/product/page/2/

Added variables and added rewrite rules:

// Adds query vars for shop post type
function gtp_add_shop_query_vars( $vars ) {
    $vars[] = 'shop-category';
    $vars[] = 'item_width';
    $vars[] = 'item_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-category/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]&page=$matches[4]&paged=$matches[5]', 'top' );
    add_rewrite_rule( '^shop-category/([^/]*)/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]', 'top' );
    add_rewrite_rule( '^shop-category/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]', 'top' );
    add_rewrite_rule( '^shop-category/([^/]*)$', 'index.php?shop-category=$matches[1]', 'top' );
}
add_action( 'init', 'gtp_add_shop_rewrite_rules', 10, 0 );
up vote 0 down vote accepted

I have added extra rewrite rules for pages with no item_width and item_height in combination with pagination as follows:

/** 
 * Adds rewrite rules for shop category
 */  
function gtp_add_shop_rewrite_rules() {

    // shop-category/category/item_width/item_height/page/2/
    add_rewrite_rule( '^shop-category\/([^/]*)\/([^/]*)\/([^/]*)\/page\/([0-9]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]&paged=$matches[4]', 'top' );

    // shop-category/category/page/2/
    add_rewrite_rule( '^shop-category\/([^/]*)\/page\/([0-9]*)$', 'index.php?shop-category=$matches[1]&paged=$matches[2]', 'top' );

    // /shop-category/category/item_width/item_height/
    add_rewrite_rule( '^shop-category\/([^/]*)\/([^/]*)\/([^/]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]&item_height=$matches[3]', 'top' );

    // /shop-category/category/item_width/
    add_rewrite_rule( '^shop-category\/([^/]*)\/([^/]*)$', 'index.php?shop-category=$matches[1]&item_width=$matches[2]', 'top' );

}
add_action( 'init', 'gtp_add_shop_rewrite_rules', 10, 0 );

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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