I'm trying to exclude the current post from an array and I know that the codex says that it's not possible to combine post__in and post__not_in in the same query...

But in wordpress everything (well, almost...) is possible and I know that must be a way to exclude the current post from my posts array.

If I can't use the together, how can I exclude current post from the array? Any hints are very welcome.

This is my current query:

$this_post = $post->ID;
$args = array('post__in' => array(17,111,108,158), 'post__not_in' => array($this_post));
$posts = new WP_Query($args);

1 Answer 1


Use array_diff() to remove the current post ID from an array of other post IDs.

$include = array( 17, 111, 108, 158);
$include = array_diff( $include, array( $post->ID ) );
$args = array( 'post__in' => $include );
$posts = new WP_Query( $args );

Edit to include a point raised by Pieter Goosen--If $include is not a hard-coded array of IDs but something that comes from the database, you should verify that it is not empty() prior to performing the query. Passing an empty array to 'post__in' will get an unfiltered list of posts, rather than returning no posts as you might expect.

  • 1
    Just a tip here, before passing any variable like $include to post__in, always make sure that you are not passing an empty array. Passing an empty array to post__in will return all posts regardless, it will not return an empty array as one would think Jun 21, 2016 at 9:47
  • @PieterGoosen so, is this code correct, Or should I do something else? Sorry to ask, but i'm not sure if I understand what you mean by "make sure that you are not passing an empty array". $include is not empty... or is it?
    – bpy
    Jun 21, 2016 at 10:00
  • 1
    @PieterGoosen Thanks for pointing that out. I've updated the answer to clarify that point. Jun 22, 2016 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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