Wordpress category.php Pagination 404 Errors Wordpress category.php Pagination 404 Errors wordpress wordpress

Wordpress category.php Pagination 404 Errors


This sounds very similar to what I experienced (In my child theme, custom permalink %category%%postname% resulted in a 404 when using pagination in category.php, only on page 2 though).

I found this solution worked really well: http://www.bamboosolutions.co.uk/fix-404-errors-wordpress-pagination/

To summarize the solution, in my child functions.php file I added this bit of code:

function custom_pre_get_posts( $query ) {  if( $query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {      $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) );  }  } add_action('pre_get_posts','custom_pre_get_posts'); function custom_request($query_string ) {      if( isset( $query_string['page'] ) ) {          if( ''!=$query_string['page'] ) {              if( isset( $query_string['name'] ) ) { unset( $query_string['name'] ); } } } return $query_string; } add_filter('request', 'custom_request');

I spent so much time reading about different causes and solutions for this error, in my case it was that WP was not requesting the correct custom category. This fix saved a lot of my time!


I had the same issue, and Lauren's fix helped me. My problem was that with this code the current page didn't change, it got stuck on page 1.

In functions.php i added the following code:

function custom_pre_get_posts($query){    if ($query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {        $query->set('page_val', get_query_var('paged'));        $query->set('paged', 0);    }}add_action('pre_get_posts', 'custom_pre_get_posts');

and in category template (category.php) i used this code:

  $paged = (get_query_var('page_val') ? get_query_var('page_val') : 1);  $query = new WP_Query(array(    'posts_per_page' => 3,    'cat' => $cat,    'orderby' => 'date',    'paged' => $paged,    'order' => 'DESC'));

for pagination i changed this code like this. Hope my solution helps:

$big = 999999999;  echo paginate_links(array(                        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),                        'format' => '/page/%#%',                        'current' => max(1, $paged),                        'prev_text' => __('Previous Page'),                        'next_text' => __('Next Page'),                        'show_all' => true,                        'total' => $query->max_num_pages                    ));


I would try switching to WP_Query first, it's less buggy with Pagination.

https://codex.wordpress.org/Function_Reference/query_posts

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

$cat = get_cat_id( single_cat_title("",false) );$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;$the_query = new  WP_Query (          array          (              'posts_per_page'=>25,              'cat' => $cat,              'paged' => $paged          ),);if ($the_query->have_posts()) : ?>

If that doesn't work, try changing your permalink structure to Post ID and see if that changes it.If neither of those work, set $cat to a category you know exists (and has 26 posts) and make sure that's not causing the problem.

Hope this helps.