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('« %link'); ?>
<?php previous_post_link('%link »'); ?>
<?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?
You’ll have to exclude the category in your
next_post_link
andprevious_post_link
calls too, as it’s using the posts data, not your specific queries data, so, this should fix it:Where 5 is the excluded category. If you need to exclude more categories, just comma separate them.