Wordpress - CUSTOM FIELDS meta_query with several keys - wordpress

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'
),
)

Related

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!

Wordpress: Meta query for custom post type not returning posts

I have a custom meta checkbox and Select dropdown on both "Post" and "Recipes". "Recipes" is a custom post type.
I'm trying to query posts where the selected option equals the author's ID AND where the checkbox is checked. This query is happening on the single-author page so i'm querying where the selected author id = get_the_id() AND the checkbox to enable this is checked.
For some reason, the part where I check if the checkbox is checked (i have the meta set to be 1, otherwise 0) is interfering with "recipe" post type:
array(
'key' => 'use_contrib_author_new',
'value' => '1'
),
The following works perfectly when querying "Post"
$args = array(
'post_type' => 'post',
'posts_per_page' => '6',
'orderby' => "date",
'order' => 'DESC',
'paged' => $paged,
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'contrib_author_choice',
'value' => get_the_ID(),
),
array(
'key' => 'use_contrib_author_new',
'value' => '1'
),
),
'meta_key' => 'contrib_author_choice'
);
On the authors page, I'm using their ID to get the selected author in the dropdown. It queries all posts where the checkbox is checked and the ID matches.
My recipe post type has the exact same meta from:
add_meta_box( 'choose_author', __( 'Choose Contributing Author', '' ), 'callback_contrib_author_post_type', array('post', 'recipes'), 'normal', 'high' );
If I change post type to "recipes", nothing is queried.
Interestingly, if I change the relation to OR, it will pull recipes, however, not all of them belong to the author since the checkbox being checked isn't accounted for.
So basically, I have the same meta on two post types and I want to use that to query 'post_type' => array('post', 'recipes')
Okay I solved this. Very very very annoying issue.
So when my custom meta checkbox is saved, it's converted to "1" for on and "0" for off.
This is so for the "Post" type. But for some reason, when I reuse this meta on my custom post type "Recipes" as I show in my code, the value is either "on" or "0".
So my new $arg is
$args = array(
'post_type' => array('post' , 'recipes'),
'posts_per_page' => '6',
'orderby' => "date",
'order' => 'DESC',
'paged' => $paged,
'meta_query' => array(
'relation'=> 'AND',
array(
'key' => 'contrib_author_choice',
'value' => get_the_ID(),
),
array(
'relation' => "OR",
array(
'key' => 'use_contrib_author_new',
'value' => '1'
),
array(
'key' => 'use_contrib_author_new',
'value' => 'on'
)
),
),
//'meta_key' => 'contrib_author_choice'
);
So incredibly annoying. If anyone has an idea why, please comment.
Thanks

WordPress meta query where key/value does not exist

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?

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.

Wordpress Meta Query to pull posts with empty meta value for custom fields

I am trying to pull custom post type with field value set as 'No' or not set (older posts for which this custom field is not set). The code i am using for query args is:
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => array(''),
'compare' => 'LIKE'
)
)));
But this meta query only pulls posts with value set as 'No' and not with posts with empty values. Please advice on how to write the meta query so all posts with No and empty values can be pulled.
"NOT EXISTS" checks will include meta_keys that don't exist.
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'com', 'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ct_Featured_C_radio_3292',
'value' => 'No',
'compare' => 'LIKE'
),
array(
'key' => 'ct_Featured_C_radio_3292',
'compare' => 'NOT EXISTS'
)
)));
Try This
http://codex.wordpress.org/Class_Reference/WP_Meta_Query
This is related to meta Query

Resources