How can I exclude one specific post from a WP_Query query? (For example, show all posts apart from a post with the ID 278)
I’ve tried the post__not_in argument but it just removes all posts..
Any help would be great.
Here is my current query
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'post_type' => 'case-study',
'paged' => $paged,
));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
Thanks
I suppose this was heavy, but to answer your original question, I’ve collected all of the posts id’s in an array in the first loop, and excluded those posts from the second loop using ‘post__not_in’ which expects an array of post id’s
The first loop displays all posts in a category, and collects the post id’s into an array.
The second loop displays all posts, excluding posts from the first loop.
The parameter you are looking for is
post__not_in
(kaiser has a typo in his answer). So the code could be like:WP_Query post__not_in documentation
You have to define the
post__not_in
arg as array. Even for a single value. And please don’t overwrite global core variables with temporary stuff.Alternative codes;
Exclude category posts
Remove posts from homepage page