Saturday, January 10, 2015

Wordpress: How to make pagination work with your custom query

If you build a custom query in Wordpress with Wp_Query(), your pagination with the following basic code might now work:

 <div class="navigation">  
 <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>  
      <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>  
 </div>  

 When you build your query you have to make sure to use the $wp_query variable as next_posts_link() and previous_posts_link() expect that.

Your query should look like this:

 <?php  
 $args = array(  
 'post_type' => 'post',   
 'posts_per_page' => 10,   
 'paged' => $paged  
 );  
 $wp_query = new WP_Query( $args );  
 ?>