Wordpress category.php Pagination 404 Errors

I am trying to display posts for categories in my category.php file paginated, but when I click the older posts button I get a 404 code. Here is the code I am currently using to request


    // Get ID of category we're currently looking at
    $cat = get_cat_id( single_cat_title("",false) );

        'cat' => $cat,
        'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )


The permalink structure I am using is /% category% /% postname% /

I read that there is a bug that will leave you with a 404 error if the "posts_per_page" parameter is set to less than the default, but that doesn't seem to be the problem. The default in my settings is 20.

Any ideas? Is this a problem with permalink settings? Couldn't / category -name / page / 2 work the same way as / blog -page / page / 2?

I also get 404 if I try to access categories like this: / category / cat-name or / blog -page / category / cat-name



source to share

3 answers

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

I found this solution to work very well: http://www.bamboosolutions.co.uk/fix-404-errors-wordpress-pagination/

To summarize the solution, in my child file, functions.php

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' ) ) );  }  } 


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 various reasons and solutions for this error, in my case WP was not asking for the correct custom category. This fix saved me!



I had the same problem and was helped by Lauren. 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 the 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 as follows. 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 to switch to WP_Query first, it's less buggy with Pagination.


query_posts () is an overly simplistic and problematic way of modifying the main query of a page by replacing it with a new query instance. It is inefficient (reruns SQL queries) and will crash unexpectedly in some cases (especially when dealing with 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
              'cat' => $cat,
              'paged' => $paged
if ($the_query->have_posts()) : ?>


If that doesn't work, try changing the permalink structure to Post ID and see if that changes. If none of these work, set $ cat to a category you know (and has 26 posts) and make sure it doesn't cause the problem.

Hope it helps.



All Articles