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'
);
Related
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.
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.
I want to create a query where all posts should be displayed where the custom field "expiration_date' is larger than the date from today.
Short form: if the expiration date of the post is reached, it should no longer displayed in the query
I tried with this snippet:
<?php
$today = date("Y-m-d");
$args= array(
'tag' => 'Pinnwand',
'meta_query' => array(
'key' => 'expiration_date',
'type' => 'DATE',
'value' => $today,
'compare' => '>'
)
);
$my_query = new WP_Query($args); ?>
the expiration date is in the format (2014-10-04) for example.
But I tried also the format "Ymd" on both sides, change the compare type, or set the type as "NUMERIC" and nothing helps. The result is, that the post will always be displayed.
It would be great if somebody could help me!
Okay I found the mistake!
The correct query needs one more array(). I don't know exactly why, but in other cases the query couldn't work with it. So here is the code
$args= array(
'tag' => 'Pinnwand',
'meta_query' => array(
array(
'key' => 'expiration',
'type' => 'DATE',
'value' => $today,
'compare' => '>'
),
),
);
$my_query = new WP_Query($args); ?>
You could do a two-query exclusion. First query finds all the posts you want to exclude, then loop through those and store the post IDs in an array, then call second query excluding them using 'post__not_in' with the ID array as the argument.
I would use the $wpdb object for this because it's very efficient.
For reference material:
http://codex.wordpress.org/Class_Reference/WP_Query and
http://codex.wordpress.org/Class_Reference/wpdb
This chunk of code SHOULD do what you're trying to do or get you close to it, and I kept your tag part of the query in there too.
// first query to find exclusions
$today = date("Y-m-d");
$exclusions = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE expiration_date < ".$today);
if ( $exclusions ) {
foreach ( $exclusions as $exclusion ) {
$excludeIDs[] = $exclusion->ID;
}
}
// second query using exclusion array
$args= array(
'tag' => 'Pinnwand',
'post__not_in' => $excludeIDs,
);
$my_query = new WP_Query($args);
You can do it simpler, as stated on http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters:
$args= array(
'tag' => 'Pinnwand',
'meta_key' => 'expiration',
'meta_type' => 'DATE',
'meta_value' => $today,
'meta_compare' => '>'
);
So, I have a search form, that returns custom posts with wp_query. Now I need to find posts with a custom field and specific value: Lets say I have multiple checkboxes named catagories[]. So when I save them it makes a serialized array in db. Now I need to search in the front end posts with lets say value 11 in it. Array looks like that: a:3:{i:1;s:2:"11";i:2;s:2:"33";i:3;s:2:"33";}. So here comes the problem, how can I retrieve information from this serialized array to find all posts with value 11. My code is:
$args = array(
'post_type'=>'paibcresume',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(),
'tax_query' => array(),
'orderby' => 'date',
'meta_key' => '',
'order' => 'DESC'
);
// BASIC SEARCH FORM
// Search by category
// rbsercategories
// rbwwcategoryhidden
if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
$args['meta_query'][] = array(
'key' => 'rbwwcategoryhidden',
'value' => $_GET['rbsercategories'],
'compare' => 'IN'
);
}
$the_query = new WP_Query( $args );
while ($the_query->have_posts() ) : $the_query->the_post();
This code works if values in data base are not a serialized array, just simple strings, but doesn't work with arrays so what do I have to do?
Ok, I didn't find the way to search thru serialized arrays, but I found the way to work around it.
So I didn't change any fields in admin, instead I added new with a loop. So I have this fields named rbwwcategoryhidden[], that create this array. I unserialized that array and created new fields for each value:
$wwCategory = get_post_custom_values('rbwwcategoryhidden');
foreach($wwCategory as $wwCategoryValue){$wwCategoryUnser = unserialize($wwCategoryValue);}
$rbWwCatSearchCounter='0';
foreach($wwCategoryUnser as $catId){
$rbWwCatSearchCounter++;
echo '<input type="text" name="rbwwcatforsearch'.$rbWwCatSearchCounter.'" id="rbwwcatforsearch'.$rbWwCatSearchCounter.'" value="'.$catId.'">';
}
So now I got new fields, I personaly have a limit of three fields (user can only check three checkboxes): rbwwcatforsearch1, rbwwcatforsearch2, rbwwcatforsearch3. And I added value to each field from that array: 11, 33 and 33.
And now I just changed the meta_query code in the front end, and it looks like that:
// Search by category
if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
$args['meta_query'][] = array(
'key' => array('rbwwcatforsearch1','rbwwcatforsearch2','rbwwcatforsearch3'),
'value' => (isset($_GET['rbsercategories'])?$_GET['rbsercategories']:array()),
'compare' => 'IN'
);
}
And it works just perfect :)
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);