0

I'm trying to achieve this permalink structure:

  • Page "about-us"
    • Page "stories"
      • Post (story CPT) "post-title"

The page "stories" is a custom page template that shows archive for custom post type "story", it needs to be a page template so I can edit the content, and should also have pagination for posts.

So domain.com/about-us/stories/ should show "story" CPT archive with pagination and domain.com/about-us/stories/story-title/ should show a single story.

I seem to get either the archive page pagination, or the post permalink to work, but not both.

If I set my CPT rewrite like

'rewrite' => array(
        'slug'                  => __('about-us/stories', 'text-domain'),
        'with_front'            => false,
        'pages'                 => true,
        'feeds'                 => false,
    )

The post permalink works just as expected, and I can follow a link to domain.com/about-us/stories/story-title/ and that's all fine, but the pagination on domain.com/about-us/stories/ won't work and gives a 404 error when changing page. If I set my CPT rewrite like

'rewrite' => array(
        'slug'                  => __('stories', 'text-domain'),
        'with_front'            => false,
        'pages'                 => true,
        'feeds'                 => false,
    )

the pagination on archive page works, but the post permalinks are now domain.com/stories/story-title and not domain.com/about-us/stories/story-title/

I also tried playing with add_rewrite_tag and add_rewrite_rule but can't seem to work my way around this problem. Is this permalink structure possible or should do I just need to settle for different url structure for single posts?

4
  • In your second rewrite rule change 'with_front' => true and try again. Make sure you save your permalinks again. Commented Dec 8, 2017 at 14:46
  • Unfortunately that won't work as my posts permalink is set to domain.com/news/%postname%/ so setting "with_front" to true would make my permalink structure domain.com/news/stories/story-title
    – ooksanen
    Commented Dec 8, 2017 at 14:52
  • Is there a reason you made stories a page rather than using the post type archive WordPress generates automatically?
    – Milo
    Commented Dec 8, 2017 at 15:44
  • Yes, there's some content on the page before the post list that needs to be editable by client.
    – ooksanen
    Commented Dec 8, 2017 at 15:49

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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