Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress pagination wp_query->max_num_pages return 0

I have problem in pagination in wordpress custom page. I using template dazzling, this is pagination function: https://github.com/puikinsh/Dazzling/blob/master/inc/template-tags.php But always $GLOBALS['wp_query']->max_num_pages is 0 (null). I try many time to change it, this is what I do now:

 $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'post',
    'category__in' => '4',
    'posts_per_page' => 3,
    'max_num_pages' => 5,
    'paged' => $paged
  );
  // create a new instance of WP_Query
  $the_query = new WP_Query( $query_args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
  <article>
    <h1><?php echo the_title(); ?></h1>
    <div class="excerpt">
      <?php the_excerpt(); ?>
    </div>
  </article>
<?php endwhile; ?>

<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
<?php dazzling_paging_nav()); ?>

<?php } ?>

<?php else: ?>
  <article>
    <h1>Sorry...</h1>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  </article>
<?php endif; ?>

What I do wrong? Any idea?

like image 550
Marcin Avatar asked Jan 08 '15 20:01

Marcin


1 Answers

Not quite sure it can help someone but just in case. I had the same issue ( $wp_query->max_pages_number returning 0) and I got stuck because of something very silly.

I realized that I had this code somewhere else in my theme :

function no_rows_found_function($query)
{ 
  $query->set('no_found_rows', true); 
}

add_action('pre_get_posts', 'no_rows_found_function');

It was be useful to improve the performance of WP_Query but the no_found_rows disables the pagination. Make sure it's not somewhere in a plugin or in your theme :)

like image 118
Laila Avatar answered Oct 02 '22 08:10

Laila