The following has been added using "generate_rewrite_rules", I have also tried using add_rewrite_rule:

'products/(.+)/(.+)/(.+)/page/?([0-9]{1,3})/?$' => 'index.php?product_cat=$matches[1]&whofor=$matches[2]&reason=$matches[3]&paged=$matches[4]',

The following option works up till page 9:


The following does not work


There are products up to 6 pages, thereafter it shows no matches found till page 9 but after 9 it directly takes me to "page not found". Similarly, if I add a parameter, to the page which was working, it takes me to "page not found".

Edit 1: the variables have also been added to query_vars.

Edit 2: Adding the actual code as requested in comments. The following is pasted in funtions.php

add_filter( 'query_vars', 'register_custom_query_vars' );
function register_custom_query_vars( $query_vars ){
    $query_vars[] = 'whofor';
    $query_vars[] = 'reason';
    $query_vars[] = 'city';
    $query_vars[] = 'stype';

    return $query_vars;
function custom_rewrite_basic( $wp_rewrite ) {
    $feed_rules = array(
          'products/(.+)/(.+)/(.+)/page/?([0-9]{1,3})/?$' => 'index.php?product_cat=$matches[1]&whofor=$matches[2]&reason=$matches[3]&paged=$matches[4]',
          'products/(.+)/(.+)/page/?([0-9]{1,3})/?$' => 'index.php?product_cat=$matches[1]&whofor=$matches[2]&paged=$matches[3]',
          'products/(.+)/page/?([0-9]{1,3})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
          'products/(.+)/(.+)/(.+)/?$' => 'index.php?product_cat=$matches[1]&whofor=$matches[2]&reason=$matches[3]',
          'products/(.+)/(.+)/?$' => 'index.php?product_cat=$matches[1]&whofor=$matches[2]'

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
    return $wp_rewrite->rules;

add_filter( 'generate_rewrite_rules', 'custom_rewrite_basic' );
  • Could you add the code where you're actually creating the rewrite rules and query vars? – bosco Mar 28 '17 at 2:24
  • @bosco added the code! – Santosh S Kumar Mar 28 '17 at 3:17
  • @SantoshSKumar You could try and use Query Monitor to determine what it is doing – adamj Mar 28 '17 at 3:26
  • @adamj, not really helping.. I see that whenever SELECT SQL_CALC_FOUND_ROWS is zero it picks up 404 template. Also, is_404 is being picked up rather than is_archive() is_paged() is_tax() – Santosh S Kumar Mar 28 '17 at 4:57
  • @SantoshSKumar It's very likely one or more URLs are conflicting. I would recommend going through the debug process of eliminating all your custom URLs except for only 1 and then trying to go past page 9. If the issue still persists, I recommend grabbing a vanilla WordPress and trying it of that just to be on the safe side. I would also recommend chucking define( 'SAVEQUERIES', true ); into your wp-config and analysing the whole query step-by-step: codex.wordpress.org/Debugging_in_WordPress#SAVEQUERIES – adamj Mar 28 '17 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.