Pagination doesn’t work in custom page template

Apparently this is very popular issue. There are already bunch of people offering answers for this. But somehow after hours of searching I still can’t get the solution. I hope someone can give me a clue here.

Currently I created a page for displaying all post from my custom post types. Here is my page template codes:

Read More
<?php

 /*
Template Name:custom post type page
*/
get_header(); ?>

<div id="primary">
  <div id="content" role="main">

    <?php   
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

    $args = array(
            'post_type'=>array('car','bicycle'),
            'posts_per_page'=> 20,
            'page'=>$paged,
            );

    $the_query = new WP_Query( $args);
    $num =  $the_query->found_posts;

     if($the_query){
     if ($the_query->have_posts()) : echo $num;
         while ($the_query->have_posts()) : $the_query->the_post();
          echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
         endwhile;

        next_posts_link( 'Next', $the_query->max_num_pages );
        previous_posts_link('Previous', $the_query->max_num_pages ); 


     else : $return_string =  'no result';  

     endif; wp_reset_query();  wp_reset_postdata();
    }

   ?>

        </div><!-- #content -->
    </div><!-- #primary -->

The problem here is when I click on the Next (or Previous), the query result remains same. I noticed that the url have show the page number (page/2/,or page/3/..etc). And the found_posts() remains same on each page, everything seem alright, only the content not showing the correct query object.

I also tried add 'max_num_pages'=>2 to the wp_query argument, but still not helping.

Anyone idea?

Related posts

Leave a Reply

2 comments

  1. Try the following and see how you go..

    <?php 
      global $paged;
      global $wp_query;
      $temp = $wp_query; 
      $wp_query = null; 
      $wp_query = new WP_Query(); 
      $wp_query->query('posts_per_page=10&post_type=your_post_type'.'&paged='.$paged);
      while ($wp_query->have_posts()) : $wp_query->the_post(); 
    ?>
    
    <!-- do your loop output here, title, content etc -->
    
    <?php endwhile; ?>
    
        <?php previous_posts_link('&laquo; Newer') ?>
        <?php next_posts_link('Older &raquo;') ?>
    
    <?php 
      $wp_query = null; 
      $wp_query = $temp; 
    ?>
    
  2. From the codex http://codex.wordpress.org/Using_Permalinks

    Sometimes navigation to second (and subsequent) pages of posts does not work as expected. Your page may generate a link to a page with one of these URIs:

    http://www.example.com/page/2/
    http://www.example.name/category/categoryname/page/2/
    http://www.example/year/month/day/page/2/
    http://www.example/year/month/page/2/
    

    The result of clicking one of those links is that the page loads with all the surroundings (header, footer, sidebar), but instead of a page of posts, there is an error message: “Sorry, no posts match that criteria.”

    This is due to a glitch in the .htaccess file that WordPress generates. To fix it, delete the contents of your .htaccess file and re-create it.