Paged Single Post doesn’t redirect to the main url after Removing Pagination

EDIT

I have played around with a possible solution to your issue. We can try the following

  • Add our own action to template_redirect where we do the following

    • Check that the current request is a paged single post page and that we do not have the <!--nextpage--> tag in the post content.

    • If everything checks out in our condition, we will do a manual redirect to the first page of the post

Here is the code:

add_filter( 'template_redirect', function()
{
    if (    is_single() 
         && false === strpos( get_queried_object()->post_content, '<!--nextpage-->' )
         && get_query_var('page') 
    ) {
        wp_redirect( get_permalink( get_queried_object_id() ) );
        die;
    }
});

IMPORTANT NOTES

This will also redirect the current paged single post page to the first page even if we make use of the content_pagination filter. The code only takes the <!--nextpage--> tag into consideration, so you will need to make sure that you will not paginate a single post with any other method than the <!--nextpage--> tag

END OF EDIT

ORIGINAL ANSWER

I’m pretty sure that this will need a lot of investigation and a lot of testing, swearing and rewriting of code to make this work.

Unfortunately when you have a URL like sample.com/tipps-for-me/4, WordPress will and has always as far as I remember from meeting WordPress in version 3.3.1, accepted that URL structure as valid, even when a post only has one page, so this is nothing new.

WordPress 4.4 saw the new content_pagination filter being added and the following piece of code to redirect_canonical

$next="<!--nextpage-->";
if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
    $page = trim( $this->query_vars['page'], "https://wordpress.stackexchange.com/" );
    $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
}

What this successfully does is, if you have the <!--nextpage--> tag in a post, every page after the last page after the <!--nextpage--> tag will 404 and redirect back to the first page. This is really the only time you will have success with URL’s like sample.com/tipps-for-me/4 when that page is a page after the last page after the <!--nextpage--> tag. If we remove the <!--nextpage--> tag, we can still successfully enter any URL without being redirected as pre WordPress 4.4

This piece of code however creates other issues which I have raised (a still open unresolved) trac ticket for, and between me and @birgire we handled the issue in the following question a week or two ago

I have asked in the mentioned trac ticket to remove the code from redirect_canonical() to avoid the new issues it created until we find a solid solution to handle pagination on single posts.

I think what really makes this very difficult now is the new content_pagination filter, as we will need to find a way to handle pages correctly that are added via the filter with and without the <!--nextpage--> tag. Another issue is that pre WordPress 4.4, people used the the_post filter to add additional page to a single post in addition to the <!--nextpage--> tag, so this adds even more issues to handle in such a way that we do not break those sites which are still relying on older valid methods

TO CONCLUDE

There will most certainly not be a solution to this in the near future. Like many other bugs that exists like all posts being returned by WP_Query on an invalid term or taxonomy or on an empty search submission or over sanitizing term names in a tax_query, there really is no concrete solution to fix these issues. My bet is that some of these issue will never be fixed and will remain till the end of days.

From SEO point of view, it does not have any affect, so it is nothing to worry about.

Leave a Comment