5

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

17

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.

  • 1
    Thank you so much - that has worked perfectly... regex really make my head hurt! You are an absolute star - thank you Rachel! You were right - the only thing I needed to do was put the pagination rewrite rule before the normal one I'd created (and thank you for tidying that up - I've learned a couple of things from this!) – Jonnyauk Oct 1 '14 at 13:52
  • You're most welcome! Glad I could help. – Rachel Carden Oct 1 '14 at 13:55
  • 2
    Quick note: To make this work, I needed to put the pagination rule first. Worked like a charm. – mrwweb May 5 '17 at 23:03
0

Order matter, you should write :

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

before

add_rewrite_rule( 'region/([^/]+)/type/([^/]+)/?', 'index.php?taxonomy=region&term=$matches[1]&post_type=$matches[2]', '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.