My setup is as follows:

  • Taxonomy called ‘region’
  • Terms in this taxonomy like ‘scotland’
  • Various post types that use this single taxonomy

I have achieved functioning pretty permalinks of the following format using the rewrite rule below:

add_rewrite_rule(
    'region/([^/]+)/([^/]+)/([^/]+)/?',
    'index.php?taxonomy=region&term=$matches[1]&post_type=$matches[3]',
    'top'
);

This give me a functioning URL like:
http://example.com/region/scotland/type/event/
which is exactly what I need (region/[scotland] = taxonomy, type/[event] = post type)

However, I’ve tried adding an additional add_rewrite_rule to deal with pagination in the following URL format (I really don’t get on well with regex!) but I just can’t get pagination to function correctly – I’m was aiming to achieve the following:

http://example.com/region/scotland/type/event/page/1
http://example.com/region/scotland/type/event/page/2
http://example.com/region/scotland/type/event/page/3

I think I’m nearly there as I have one rewrite rule functioning correctly – but after trying lots of different things I’ve just not been able to achieve the correct regex/rewrite rule combination to achieve the pagination. The not-so-pretty permalink for this query, which functions correctly is:

http://example.com/index.php?taxonomy=region&term=scotland&post_type=event&paged=1

2 s
2

Have you tried:

The main one:

add_rewrite_rule( 'region/([^/]+)/type/([^/]+)/?', 'index.php?taxonomy=region&term=$matches[1]&post_type=$matches[2]', 'top' );

For pagination

add_rewrite_rule( 'region/([^/]+)/type/([^/]+)/page/([0-9]{1,})/?', 'index.php?taxonomy=region&term=$matches[1]&post_type=$matches[2]&paged=$matches[3]', 'top' );

Out of curiousity, I replaced one of the ([^/]+) with the literal ‘type’ since that doesn’t seem to be a dynamic variable.

If the pagination doesn’t work, try switching the order of when it’s declared, i.e. before or after your other rewrite rules. Alot of issues have to do with what order they’re declared which effects which rule is run first.

Also, the Rewrite Rules Inspector plugin can be super helpful in helping you figure out which rewrite rules are being run for specific URLs.

Leave a Reply

Your email address will not be published. Required fields are marked *