Ignoring a category in WP_Query still shows the links in next_post_link()?

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:

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

Related posts

Leave a Reply

1 comment

  1. 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.