I’m trying to use get_posts() to pull articles that have not been assigned a specific custom field. For example (not working):
$articles = get_posts(array(
'post_parent' => 0,
'post_type' => 'custom-type',
'meta_query' => array(
array(
'key' => 'alternate_page',
'value' => array(0, '', null),
'compare' => 'IN'
),
)
));
Basically, I want all ‘custom-type’ articles that don’t have a parent and don’t have the ‘alternate_page’ custom field assigned.
I’ve read elsewhere about doing two queries, one for all articles that have the custom field, getting and array of their IDs, and using the ‘__post_not_in’ parameter, but this seems tedious.
Is it possible to do in one query like above? Thanks.
You can do it with a
posts_where
filter and a subquery. Note that you have to explicitly setsuppress_filters
to false, asget_posts
normally ignores filters: