I have a custom page that uses a rewrite rule. The "non-pagination" rewrite works fine:
add_rewrite_rule(
'^brand-list/([^/]*)/([^/]*)/?',
'index.php?pagename=brand-list&brand=$matches[1]§ion=$matches[2]',
'top'
);
basically a url like this /pagename=brand-list&brand=MyBrand§ion=MySection
ends up like this, /brand-list/MyBrand/Mysection
However, when using pagination the page number is never returned to wordpress so $paged
on page 2 is still 0
add_rewrite_rule(
'^brand-list/([^/]*)/([^/]*)/page/([0-9]+)?',
'index.php?pagename=brand-list&brand=$matches[1]§ion=$matches[2]&paged=$matches[3]',
'top'
);
this url, /pagename=brand-list&brand=MyBrand§ion=MySection&paged=2
, ends up like this, /brand-list/MyBrand/Mysection/page/2
but still shows page 1. Checking the $paged
value it outputs as 0, it should be 2.
If I removed the rewrite rules and /pagename=brand-list&brand=MyBrand§ion=MySection&paged=2
is passed page 2 is shows and $paged
value is 2.
With the rewrite rule in place, if you have over the page numbers you get this:
/brand-list/MyBrand/Mysection/?paged=2
Obviously the rewrite rule is the problem but I'm not sure what part I'm missing.
If I use this url:
/pagename=brand-list&brand=MyBrand§ion=MySection&paged=2
with the rewrite all it results in:
/page/2/pagename=brand-list&brand=MyBrand§ion=MySection
and works! but my url structure is incorrect.
----- edit ------
my current function
function rewrite_rule_brands() {
global $wp_rewrite;
add_rewrite_tag('%brand%','([^/]*)');
add_rewrite_tag('%section%','([^/]*)');
add_rewrite_rule('^brand-list/([^/]*)/([^/]*)/page/([0-9]+)?','index.php?pagename=brand-list&brand=$matches[1]§ion=$matches[2]&paged=$matches[3]','top');
add_rewrite_rule('^brand-list/([^/]*)/([^/]*)/?','index.php?pagename=brand-list&brand=$matches[1]§ion=$matches[2]','top');
$wp_rewrite->flush_rules();
}
add_action( 'init', 'rewrite_rule_brands' );