I have built a complex query which should do 3 things:

  1. select posts with ids in the $additional_ids array using post__in;
  2. ignore posts with ids in the $ignore_ids array using post__not_in;
  3. select posts with a combination of cpt values (ie. is an animal, is blue and eats grass) using meta_query.

Here's what I have so far:

$args_assets = array(
        'post_type' => 'assets',
        'posts_per_page' => -1,
        'post__in' => $additional_ids,
        'post__not_in' => $ignore_ids,
        'meta_query' => array(
            'relation' => 'AND',
            $query_animal,
            $query_color,
            $query_food,
        ),
        'orderby' => 'title',
        'order' => 'asc',
        );

The query works fine WTHOUT the 'post__in' => $additional_ids line. As soon as I add the post__in argument, the query returns an empty result.

Is it possible to combine post__in with a meta_query? If so, what's the best method?

Thanks in advance for any help!

  • Seeing a typo in your post__not_in value. Var name is misspelled? – jdm2112 Dec 13 '16 at 17:37
  • @jdm2112 Thanks! Is correct in my code. – Richard Tinkler Dec 13 '16 at 17:40
  • Comment out all your query args except the meta query and see what you get. Looks like a format problem in the args of your meta. The $query_animal and others should be in an array. – jdm2112 Dec 13 '16 at 17:42
  • @jdm2112 $query_animals etc are arrays. If I comment out post__in everything works which makes me wonder if there's a problem with my logic. The query should select the post__in IDs AND the ids from my meta_query. Perhaps the relation between post__in and meta_query is wrong? – Richard Tinkler Dec 13 '16 at 18:02
up vote 4 down vote accepted

post__in and post__not_in are mutually exclusive.

Note: you cannot combine post__in and post__not_in in the same query.

http://codex.wordpress.org/Class_Reference/WP_Query

Either make the query with post__in and meta_query combination or make the query with post__not_in and meta_query combination. Any of them but not the union of them.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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