Reputation: 249
My custom post type order has a meta key 'status'. A post can have multiple statusses e.g. ordered, paid, completed.
I would like to query all unpaid orders. These don't have a meta key/value pair of status/paid.
I can query all paid orders with ...
array(
'key' => 'status',
'value' => 'paid',
'compare' => '=='
)
... it works
But when I try to query all unpaid orders with ...
array(
'key' => 'status',
'value' => 'paid',
'compare' => '!='
)
... WordPress also returns post which do have the status/paid pair, because they also have the status/ordered pair, which returns true.
Is there a way to fetch posts which don't have a certain meta_key / meta_value pair? Or should I write my own query using wpdb()?
Kind regards, Tom
Upvotes: 1
Views: 1369
Reputation: 13890
meta_query
can take in multiple arrays, can you just do:
'meta_query' => array(
array(
'key' => 'status',
'value' => 'paid',
'compare' => '!=',
//'compare' => 'NOT EXISTS', //Perhaps this instead?
),
array(
'key' => 'status',
'value' => 'ordered',
'compare' => '!=',
)
)
Though reading your question it almost sounds like you want results that are status:ordered=true
right?
Upvotes: 0