Pagination gives 404 error

All we need is an easy explanation of the problem, so here it is.

It’s seems that this bug of WP never was solved by WP team.

I have custom post-type, my url structure is /%category%/%postname%/ And if i click on 2nd page i will get 404 error.

I tried everything i found on internet, plugins, codes.. i even changed my pagination base url to NOT be page, so wp don’t think that i’m searching for post-type named page. But still nothing.

And yes i did the resave process of permalinks, i’ve tried restoring to deafault and then go back, still nothing…
Downloaded wp_navi still the same..

Is there anything i can do to solve this?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Have you tried this?

I had gotten the same problem before. I did these steps, and it works for me.

I use this code in my functions.php

// New method of WordPress Query since 3.4.1
if ( ! function_exists( 'ucc_add_cpts_to_pre_get_posts' ) ) {
    function ucc_add_cpts_to_pre_get_posts( $query ) {
    if ( $query->is_main_query() && ! is_post_type_archive() && ! is_archive() && ! is_search() && ! is_singular() && ! is_404() ) {
        $my_post_type = get_query_var( 'post_type' );
        if ( empty( $my_post_type ) ) {
            $query->set('post_type', 'your_post_type_name'); // replace your_post_type_name with yours
        }
    }
}
}add_action( 'pre_get_posts', 'ucc_add_cpts_to_pre_get_posts' );

Then, I use this in my single-{post-type}.php

<?php $paged = 1;
  if ( get_query_var('paged') ) $paged = get_query_var('paged');
  if ( get_query_var('page') ) $paged = get_query_var('page'); 
  if (have_posts()): 
?>


<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $loop = new WP_Query( array(
    'post_type' => 'your_post_type_name', // replace your_post_type_name with yours
    'posts_per_page' => PER_PAGE_DEFAULT, // Displaying number posts per page equals to Reading Settings in WP admin
    'paged'=>$paged
  )); 
?>

<?php query_posts( '&post_type=your_post_type_name&paged=' . $paged );  ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

  <!-- The content is here -->

<?php endwhile; else: ?>
  <p>Not Found</p>
<?php endif; wp_reset_query(); ?>

<div class="navigation">
 <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?> 
</div>

In my case, I use wp page numbers plugin.

Hopefuly, it can solve your problem.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply