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%/
/blog/%postname%/
?/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 exceptpost
? Or is it possible to add/blog/
as the slug to thepost
post type registration through a hook or action?with_front
set tofalse
so they don't append thepost
front value. If you don't register those post types with your own code, then use theregister_post_type_args
filter like in this answer.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.