WordPress meta query where key/value does not exist - wordpress

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

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?

Related

Query posts with custom field is array

I'm using ACF to create custom field of select type. I used like this to create custom select field like this:
1 : Anh
2 : Hai
3 : Hoang
4 : May
....
12 : Lan
I use this query to search posts have value is 1 (Anh) like this:
URL for query : /?thename=1
$thename = $_GET['thename'];
$args = array(
'post_type' => 'interviews',
'post_status' => 'publish',
'posts_per_page' => $perpage,
'paged' => get_query_var('paged'),
'meta_query' => array(
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
)
I searched like this but it returns wrong results. I think the value of thename field is array (value and label) then It is not possible to use query above.
So can you suggest me to do with this search? Thanks.
Your meta query should be an array within an array... update the meta_query like below:
'meta_query' => array(
array(
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
)
)
According to the meta_query documentation meta_query must contains one or more arrays, so your query must be something look like this :
'meta_query' => [
[
'key' => 'thename',
'value' => $thename,
'compare' => 'LIKE',
]
]

Wordpress - CUSTOM FIELDS meta_query with several keys

I have an issue with multiple meta_query keys in custom fields. So, in my wordpress I've added custom field with checkbox named 'Unavailable' so if the checkbox is clicked, the post won't appear.
so, my meta_query looked like this:
'meta_query' => array(
array(
'key' => 'Unavailable',
'compare' => '=',
'value' => '0'
)
)
and it worked fine. recently i've added second custom field called 'Invisible', also with checkbox property. Mainly, this field does the same as 'Unavailable' one, with one difference. The issues occures when I try to add an operator for those two keys. So if unavailable or invisible is checked, do something.
I tried, but it doesn't work:
'meta_query' => array(
'relation' => 'OR'
array(
'key' => 'Unavailable',
'compare' => '=',
'value' => '0'
),
array(
'key' => 'Invisible',
'compare' => '=',
'value' => '0'
),
)

Get Posts based on serialized meta value

I have a series of posts and all have a meta_key with the value of "owner" and meta_value with serialized data such as "a:3:{i:0;s:3:"325";i:1;s:2:"41";i:2;s:2:"29";}"
meta_key owner
meta_value a:3:{i:0;s:3:"325";i:1;s:2:"41";i:2;s:2:"29";}
I am trying to figure out how to properly use get_posts() to return all the posts that have a meta_key with the value of owner and the meta_value containing a specific value such as 41
$args = array(
'post_type' => 'rp_applications',
'nopaging' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'compare' => '=',
'key' => 'archived',
'value' => '0000-00-00'
),
array(
'compare' => '=',
'key' => 'owner',
'value' => ???? WHAT DO I PUT HERE ????
)
)
);
$applications = get_posts($args);
This sounds like you should be storing the "Owner" as a taxonomy term instead of meta data. It would give you easier and faster querying abilities as well.
That said, it's definitely possible to do what you want. I'd be cautious though, because the postmeta table by default is not indexed, so a large table with meta queries run against it will slow down your site quite a lot. You may want to consider adding a partial index to the table if it's going to be even remotely large (or again, switch to Taxonomy Terms instead of post meta for this).
If you still care to use a meta query, take a look at the compare options available in the WP_Meta_Query() docs.
One of the available options is REGEXP, so you could do something like:
$args = array(
'post_type' => 'rp_applications',
'nopaging' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'compare' => 'REGEXP',
'key' => 'owner',
'value' => sprintf( 'a:\d:{i:0;s:\d:"\d.*?";i:1;s:%d:"%d".*', strlen($owner_id), $owner_id )
),
array(
'compare' => '=',
'key' => 'archived',
'value' => '0000-00-00'
)
)
);
Of course this method only works if the serialized data is in the same format on each one, and you'd need to adjust the regular expression if not - but that should be enough to get you started!

ACF query posts where post object is NULL / false

The code below should hopefully make it clear what I'm trying to achieve. The key issue is with the second array in the meta_query. I am trying to find posts where the field 'alias' has not had a post_object set.
When running the query with var_dump( get_field('alias') ); the results returned are 'NULL'. I can't figure out how to query based on a NULL post object field. Pointers would be really appreciated.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'game',
'value' => 'baseball',
'compare' => '='
),
array(
'key' => 'alias',
'value' => NULL,
'compare' => '='
)
)
);
You're comparing it wrong. You can't check for null in meta_query. Try this:
array(
'key' => 'alias',
'compare' => 'NOT EXISTS'
)
This is basically the SQL way of saying is null.

WP_Query multiple values with Meta key?

I am really facing so much problem in fetching result of multiple values and single meta key.
Example:
I have 10 Post of 'EGG' word.
I have 5 Post of 'Butter' word.
Now i am using below query to fetch result. It suppose to show 15 post in result but its not showing total result.
$args = array(
'numberposts' => -1,
'post_type' => 'recipes',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ingredients_repeat_%_name',
'compare' => 'LIKE',
'value' => 'Butter',
),
array(
'key' => 'ingredients_repeat_%_name',
'compare' => 'LIKE',
'value' => 'Egg',
),
)
);
Instead of using the numberposts try posts_per_page instead. You can learn more about WP_Query

Resources