Meta Query - Compare two meta values in one query - php

I was looking on another question , and thought of changing the original query to compare two meta_key values.
This question deals with finding WC coupons that reached their usage limit.
There are two values stored as post meta data: usage_limit and usage_count.
I want to find all the coupons that the usage_count is equal or greater than the usage_limit. Something like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'usage_count',
'value' => [meta_key='usage_limit'] value here,
'compare' => '>='
)
)
);
I didn't find any solution for this type of query, but thought there might be an elegant way.

Related

WordPress - get_posts by meta_query where key CONTAINS certain string

I am trying to run a PHP get_posts query in WordPress to target posts with a meta data key that CONTAINS a certain string. The issue is, I don't know the full key name as it is created independently by another plugin but all keys have in common that they start with "_cegg_data".
Sometimes the field will be called "_cegg_data_ae_googlecom", other times "_cegg_data_ae_youtubecom" and so on.
Is there a way to run a query posts with a meta_query that contains "_cegg_data" in the key (value unknown as well)?
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_cegg_data{{anything here}}'
)
)
);
$postslist = get_posts( $args );
You can add a LIKE comparison for partial string macthing.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_cegg_data',
'compare' => 'LIKE',
'value' => '_cegg_data%'
)
)
);
$postslist = get_posts($args);
The % will work as a wildcard matching any number of characters, so this will get every product post where the key starts with _cegg_data.

How to do meta_query when the value of a meta_key is an array?

I am working on Wordpress website and hit an obstacle when I tried to do the following query:
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'ad_info',
'value' => // check again with weight between 50 and 100,
'compare' => //compare operator,
),
)
);
the value of the key ad_info is: Array ( [weight] => 48 [height] => 160) and I want to check if the value of weight in this array is between 50 and 100.
Can anybody help me on this problem?
Thanks
In mysql array values saved as strings, and it make problem for WP_Query filtering.
So best solution in this problem - change saving method to 2 keys:
ad_info_width and ad_info_height.
In that way you can use meta_query between compare.

Only get pages where a field matches one of a set of values

I'm trying to create a storelocator in wordpress where I use zipcodes to filter for distance. I then create I string with all zipcodes that are within range and make a string with all those zipcodes separated by comma's. This works fine and returns something like this:
5062,5063,5061,4163,5060,5059,5056,5076,5066,5070,5071,5074,5268,5034,5035,5037,5027,5033,5032,5031,5030,5029,5028,5038,5039,5036,5049,5048,5047,5046,5045,5044,5043,5042,5041,5040,5026,5025,5010,5009,5008,5007,5006,5005,5004,5003,5002,5001,5000,5011,5012,5013,5024,5023,5022,5021,5020,5019,5018,5017,5016,5015,5014,5296
Now I want to get posts where the zipcode CONTAINS one of these values. I figured something like this would work:
$query_adresses = array (
'order' => 'ASC',
'cat' => $_GET["cat"],
'post_type'=> 'adressen',
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'postcode',
'value' => array($final_target_zips),
'compare' => 'IN',
)
)
);
Sadly it doesn't work and just returns all posts that match the other arguments. Keep in mind that zipcodes in the posts consist of the numbers but also have two letters attached to them like: 5021GH. Only the numbers need to be checked though. I hope someone can help me out.

Sort Wordpress Posts by One Custom Field , while excluding all with another value

i have my wordpress posts with two custom fields
'episode_number'
'season_number'
i want to select all posts with season_number=5(example) , and sort them by episode_number DESC
i've written this
$max_season=5;
$args = array(
'meta_key' => 'season_number',
'meta_compare' =>'==',
'meta_value' => $max_season,
'orderby' => 'episode_number',
'order' => 'DESC',
);
$wp_query = new WP_Query($args);
the selection based on season_number works , but i can't order them , with this code wordpress order them based on the time of publishing
anyone can help me?
I believe WordPress will only query the specified metadata you requested, which is key=season_number, value=5 (the season number). It will not query the post metadata for the key episode_number, so there is no way you can sort by that field directly in the SQL query.
You either need to loop the result of the query and get each item's metadata for key=episode_number and populate a new sorted array accordingly or figure out a way to query also this metadata in a single query as well.
For the second one, I have no idea how this will work.
My original answer was off the mark, I misread the question
Try something like this:
$max_season=5;
$args = array(
'meta_key' => 'episode_number',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'season_number',
'value' => array($max_season),
'compare' => 'IN',
)
)
);
$wp_query = new WP_Query($args);

Wordpress query with several meta values as condition

First time using wordpress query so not sure how to handle this problem. I want to get post titles where meta_key is 'tag' and meta_values are 'A, B, C'. Something similar to IN() function of sql query. How should I do to achieve that?
This is my code so far but it is not working as it ignores condition.
//$tags consists of my meta_value ( A, B, and C)
foreach ($tags as $input) {
$data[] = array( 'meta_key' => 'tag', 'meta_value' => $input, 'orderby' => 'post_date' );
}
$query = new WP_Query( $data );
Thanks in advance for your kind help.
Read the Custom Field Parameters section on the WordPress Codex for WP_Query.
The problem is you are rebuilding your entire $data array on each loop. This passes invalid parameters to WP_Query. So I imagine your getting undefined results.
You simply want to set meta_value directly or specify a meta_query parameter similar to the IN clause you described.
Here is the answer:
$args = array(
'meta_query' => array(
array(
'key' => 'tag',
'value' => $tags,
'compare' => 'IN'
)
),
'orderby' => 'post_date'
);

Categories