Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix pagination after Wordpress 5.5 update

Tags:

php

wordpress

After updating my website to Wordpress 5.5 pagination broke.

If example.com/news/ is the news page for my website, then following example.com/news/2/ (that opened the second page of posts before the update) redirects me to example.com/news/.

It is a known problem of the 5.5 update, but I didn't manage to fix it by changing the variable's name.

Here is how I use pagination on my website:

The query part:

$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$query = new WP_Query( array(
   'posts_per_page' => 5,
   'paged' => $page
   ));
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

The pagination part:

echo paginate_links( array (
   'base' => get_permalink( get_option( 'page_for_posts' ) ) . '%_%',
   'total' => $query->max_num_pages,
   'current' => max( 1, get_query_var( 'page' ) ),
   'format' => '%#%',
   'show_all' => false,
   'type' => 'plain',
   'end_size' => 2,
   'mid_size' => 1,
   'prev_next' => true,
   'prev_text' => '←',
   'next_text' => '→',
   'add_args' => false,
   'add_fragment' => '',
));

wp_reset_postdata();

Using either of this:

remove_filter('template_redirect', 'redirect_canonical');

remove_action('template_redirect', 'redirect_canonical');

causes 404 error at example.com/news/2/.

like image 708
Piotr Markielau Avatar asked Aug 07 '26 22:08

Piotr Markielau


1 Answers

Temporary fix without touching the core: disable the 404 handling for paged urls via filter pre_handle_404 in your theme's functions.php:

function pre_handle_404($preempt, $wp_query)
{
    if (isset($wp_query->query['page']) && $wp_query->query['page']) {
        return true;
    }

    return $preempt;
}
add_filter( 'pre_handle_404', 'pre_handle_404', 10, 2 );
like image 145
Alex Avatar answered Aug 09 '26 13:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!