1

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?

2

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' );  
endwhile; 

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

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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