WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have a WP_Query like so:

$homepage = new WP_Query( 'posts_per_page=1&cat=-5' );

and in my while loop, I have a call to next_post_link() and previous_post_link(), which are both showing posts from the excluded category. Here is what my code looks like next:

<?php while ( $homepage->have_posts() ) : $homepage->the_post(); ?>
 <?php next_post_link('&laquo; %link'); ?> 
 <?php previous_post_link('%link &raquo;'); ?> 
<?php endwhile; ?> 

According to the wordpress docs on next_posts_link & previous_posts_link, these functions are based on the current query:

Prints a link to the next set of posts within the current query.

So does anybody have an idea of why next_post_link() and previous_post_link() would show posts that should be excluded while the rest of the loop works as expected?

share|improve this question
up vote 2 down vote accepted

You'll have to exclude the category in your next_post_link and previous_post_link calls too, as it's using the posts data, not your specific queries data, so, this should fix it:

while ( $homepage->have_posts() ) : $homepage->the_post(); 
    next_post_link( '&laquo; %link', '%title', false, '5' );  
    previous_post_link( '%link &raquo;', '%title', false, '5' );  

Where 5 is the excluded category. If you need to exclude more categories, just comma separate them.

share|improve this answer
Sadly, they are both still displaying posts from category 5. – Kyle Hotchkiss Sep 30 '11 at 2:02
Now in my specific setup, setting that false to true cleared up the issues because of how my categories were set up, thanks still! – Kyle Hotchkiss Sep 30 '11 at 2:08

Your Answer


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.