Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Pagination not working with custom loop

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:

<?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?

share|improve this question
    
Please search before posting questions. This one has been asked and answered many, many times. –  Chip Bennett Oct 23 '12 at 21:27
add comment

marked as duplicate by Chip Bennett, brasofilo, kaiser, toscho Oct 24 '12 at 7:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 1 down vote accepted

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; 
?>
share|improve this answer
    
Tried, but not working as well. –  dev-jim Oct 24 '12 at 14:41
    
@tc.k Small edit, adding two globals. Try again and see how you go this time. –  userabuser Oct 24 '12 at 14:43
    
no luck. The problem is the searched query remain the result on every paginated page. –  dev-jim Oct 24 '12 at 14:50
    
Ok,finally i found the problem. It was a silly mistake, turns it is paged not page I need to use in the argument. –  dev-jim Oct 24 '12 at 15:26
    
@tc.k That's correct. I didn't notice the missing d in your argument either, but yes 'paged='.$paged as a rule. Glad its all working for you now. All the best. –  userabuser Oct 24 '12 at 15:37
add comment

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.

share|improve this answer
    
+1, nice catch. –  Johannes Pille Oct 23 '12 at 18:35
    
I read that just 2 days back ;) –  Mridul Aggarwal Oct 23 '12 at 18:36
    
@Mridul Aggarwal, tried your method, but still not working. –  dev-jim Oct 23 '12 at 18:47
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.