Ive set up a page that users can search posts by tags, four categories, for example. By Room - By Style - By Colour - By Feature, each has an undefined number of sub categories, depending on how many tags are associated with each category (these categories and sub categories are hard coded) Works fine, and I have a rewrite rule of:

 add_rewrite_rule('discover/interiors/interior-results/(.+)', 'index.php?pagename=discover/interiors/interior-results&tag1=$matches[1]', 'top' );

I've done it this way as I dont know how many variables a user will select, so it could be:



As I said, this works fine, but am finding now that as the number of posts increase, I tried adding pagination, which after a lot of head scratching on why its not working, realised that now the pagination is being read by the rewrite rule as a tag (/page/2/)

Is it possible to create a rewrite rule to read x number of variables then the last being the pagination?

EDIT Before posting and after writing, I saw other post on here similar, where for them, the rewrite rule of:

 add_rewrite_rule('discover/interiors/interior-results/(.+)/page/?([0-9]{1,})/?$', 'index.php?pagename=discover/interiors/interior-results&tag1=$matches[1]&paged=$matches[2]', 'top' );

Worked for them and I can sort of see where they were going, but unfortunately, this breaks the first page (404) and the page/2 is actually contents of page 1 so.. If anyone can guide me in the right direction and as always any help is much appreciated.


Got it to work, changing the regex from (.+) to (.*)

Maybe helpful for others searching same rewrite on search and pagination.

 add_rewrite_rule('discover/interiors/interior-results/(.*)/page/([0-9]{1,})/?', 'index.php?pagename=discover/interiors/interior-results&tag1=$matches[1]&paged=$matches[2]', 'top' );
 add_rewrite_rule('discover/interiors/interior-results/(.*)', 'index.php?pagename=discover/interiors/interior-results&tag1=$matches[1]', 'top' );

Your Answer

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

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