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.

In WordPress, I have a page for News that is using a specific page template I created. This page is populated by news posts assigned to the news category. I need to show only two of the latest news posts on this page with Previous / Next links at the bottom if users want to read more news. However, the next / previous links are not working. Clicking on them does go to /page/2 but it's shows the same posts. Here is my code .. any ideas / help appreciated!

<div id="primary">
<div id="content" role="main">
            <div id="news">
    <?php query_posts( "category_name=news&orderby=date&order=ASC&posts_per_page=2" ); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    <p class="post-date">Posted on <?php the_date(); ?>
                    <?php the_excerpt(); ?>
                <?php endwhile; ?>
                <?php endif; ?>

                <div id="more-posts">
                    <div class="previous-link"><?php previous_posts_link("< previous news") ?></div><div class="next-link"><?php next_posts_link ("more news >") ?></div>
                </div>
             </div>
    </div><!-- #content -->
    </div><!-- #primary -->
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

When you perform a custom query such as this one, you have to do a bit of a "hack" to get pagination to work properly.

Change this:

<?php 
query_posts( "category_name=news&orderby=date&order=ASC&posts_per_page=2" ); 
?>

...to a WP_Query call:

<?php 
$news_query = new WP_Query( "category_name=news&orderby=date&order=ASC&posts_per_page=2" ); 
?>

And then you need to *move your custom query object into the $wp_query global`:

// Move $wp_query into a temp holder
$temp = $wp_query;
// Nullify the global object
$wp_query = null;
// Now move your custom query into the 
$wp_query = $news_query;

Then, update your loop initiation, from this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

...to this:

<?php if ( $news_query->have_posts() ) : while ( $news_query->have_posts() ) : $news_query->the_post(); ?>

This should cause the pagination to work as expected.

When you close your loop:

<?php endwhile; ?>
<?php endif; ?>

...just restore the original $wp_query global:

<?php endwhile; ?>
<?php endif; ?>
<?php $wp_query = $temp; ?>

And you should be good to go.

share|improve this answer
    
Hi Chip, thanks for the detailed answer! For some reason my pagination is still not working. I made all the code changes per your post, but when i navigate from page to page with the previous / next links it shows the same posts, they don't change. Am I missing something? Is my pagination code above correct and should it come after the <?php $wp_query = $temp; ?> code? –  IntricatePixels Oct 25 '11 at 18:50
    
Your pagination code is outside the Loop, right? –  Chip Bennett Oct 25 '11 at 19:49
    
I believe it is. My code is the same as in my original post + your changes. The pagination code right now is after <?php $wp_query = $temp; ?> –  IntricatePixels Oct 25 '11 at 20:02
    
It was missing page=$paged. Works now! <?php $news_query = new WP_Query( "category_name=news&orderby=date&order=ASC&posts_per_page=1&paged=$paged" ); ?> –  IntricatePixels Oct 25 '11 at 20:16
    
Should the $wp_query = $temp; code go before or after the pagination code? –  jjeaton Mar 10 '12 at 9:00
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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