Managed to get this working with a little help from Debug This plugin and some little hints from similar questions. At least so far, after some quick testing, I haven’t come across any negative side effects.
The problem, if I understood correctly, is that if I have my cpt rewrite set to same slug as my archive page /about-us/stories
, WP rewrites pagination requests /about-us/stories/page/n/
to a CPT named “page” which in this case returns 404.
To fix this I needed to make a custom rewrite rule to check if we’re querying for a page number and rewrite it accordingly:
function wpse288130_rewrite_rules() {
add_rewrite_rule('^(.?.+?)/page/(\d)$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
}
add_action('init', 'wpse288130_rewrite_rules');
Where the regex’s first capturing group (.?.+?)
catches the page name about-us/stories
and the second group (\d)
catches any digit after /page/
In the second parameter of add_rewrite_rule
we tell WP to rewrite this as index.php?pagename=about-us/stories&paged=[page number]
Third parameter ‘top’ tells WP to prioritize this rewrite above default rewrites.