1

I have custom rewrite rules in place so that the posts page URL can be /blog/ and all posts have the URL structure /blog/post-title-here/, this works great, however I am getting a 404 error on the /blog/ page pagination when it is structured like /blog/page/2/ -- I've seen many topics and questions on this around StackOverflow, however it appears everything answered is for categories to be structured like /blog/cat-name/page/2/ where I just need it /blog/page/2/

Here is my rewrite function in place to append /blog/ to post URLs:

add_action( 'generate_rewrite_rules', 'site_add_blog_rewrites' );
function site_add_blog_rewrites( $wp_rewrite ) {
  $wp_rewrite->rules = array(
    'blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
    'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
    'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
    'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
    'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
    'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
    'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
    'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
    'blog/([^/]+)/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
    'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
    'blog/([^/]+)/(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
    'blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
    'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
    'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' =>     'index.php?attachment=$matches[1]&feed=$matches[2]',
    'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' =>     'index.php?attachment=$matches[1]&cpage=$matches[2]',
  ) + $wp_rewrite->rules;
}

And then I am converting the post permalinks using:

function site_filter_post_link($permalink, $post) {
  if ($post->post_type != 'post') {
    return $permalink;
  }
  return 'blog'.$permalink;
}
add_filter('pre_post_link', 'site_filter_post_link', 10, 2);

Am I missing the proper rewrite to allow the URL structure /blog/page/2/ or is this not possible because WP is thinking /page/ should be a post when it is in this structure? Any help would be greatly appreciated!

NOTE: I cannot have my permalinks structure /%category%/%postname%/. The permalinks must stay at /%postname%/

8
  • 1
    Why not just set permalink structure to /blog/%postname%/?
    – Milo
    Commented Jul 30, 2018 at 14:40
  • Well, there are 11 other custom post types that can't have /blog/ in the URL. That structure is only needed for the 'post' post type, nothing else. Do you know of an easy route to remove that from all post types except post? Or is it possible to add /blog/ as the slug to the post post type registration through a hook or action?
    – Ty Bailey
    Commented Jul 30, 2018 at 14:53
  • Custom post types should be registered with with_front set to false so they don't append the post front value. If you don't register those post types with your own code, then use the register_post_type_args filter like in this answer.
    – Milo
    Commented Jul 30, 2018 at 15:38
  • That removes the post type slug from the URL too though? For example, if a post type is food I would need that post URL to be /food/food-post-name. I should also note I have a rewrite in place to remove a custom taxonomy base slug from the URL too.
    – Ty Bailey
    Commented Jul 30, 2018 at 16:49
  • No, with_front and slug are different things.
    – Milo
    Commented Jul 30, 2018 at 20:45

1 Answer 1

0

If anyone still has this issue (this post is quite old), I found a very helpful snippet in this blog post: https://www.grzegorowski.com/wordpress-rewrite-rules

/**
 * Reprioritise pagination over displaying custom post type content
 */
add_action('init', function() {
  add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
});

This will ensure WP uses the pagination rule prior to the post type query rules

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.