I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
Related
I have a custom post type, that has price in 2 different meta keys, based on product type. I want to filter the query so that all products can be displayed in either asc or desc order. Here is my query and it is not sorting the results.
$args = array(
'post_type' => 'ars-products',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'ars_product_price',
'compare' => 'EXISTS',
),
array(
'key' => 'ars_product_min_amount',
'compare' => 'EXISTS',
)
),
array(
'relation' => 'OR',
array(
'key' => 'ars_product_type',
'value' => 'one_time',
'compare' => '=',
),
array(
'key' => 'ars_product_type',
'value' => 'donation',
'compare' => '=',
),
),
),
'orderby' => array(
'ars_product_price' => 'ASC',
'ars_product_min_amount' => 'ASC'
)
);
$products_data = new \WP_Query( $filters );
Edit: it does sort perfectly, if I only use one meta key ars_product_price.
I am trying to query custom post type but I keep getting no result. Is my meta_query the culprit? What is wrong with this code?
I am trying to spot the issues but i cannot find anything.
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => $priority,
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);
Output
Array()
I solved the problem by using array for 'levelof_priority'.
The field type for 'levelof_priority' is radio button whereas the other 2 are text field.
Below are my final code that's working
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => array($priority),
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);
I have the following query that I would like to pass an array of values to:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'LIKE'
)
)
)
);
For example, I would like to pass like this:
$customer_names = array('John','Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
The query will also need to handle the possibility that all customers could have data for each date. In pseudo SQL, my best guess would be:
SELECT * FROM WORDPRESS_POSTS WHERE _customer_names = (John OR Tom OR Simon) AND customer_dates = (20161225 OR 20161226 OR 20161227)
However, at the moment, even when I remove the date restriction, I can't find any posts. Hence, I wanted to confirm my logic is correct.
Try like this:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_customer_names',
'value' => 'John',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Tom',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Simon',
'compare' => '='
)
),
array(
'relation' => 'OR',
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
)
),
)
)
);
if you want to use array in values you need to use compare with IN clause.
OR if you want to match with exact values you can try #Ravendra Patel's solution.
Also use WP_QUERY because it comes with main query and provide more help to identify the issue
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$args = array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => 'IN'
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
echo 'SQL: '.$query->request.'<br>'; // your query against args
try like this:
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$cm_metaq = array();
foreach($customer_names as $cm){
$cm_metaq[] = array('key' => '_customer_names', 'value' => $cm, 'compare' => '=');
}
$cd_metaq = array();
foreach($customer_dates as $cd){
$cd_metaq[] = array('key' => '_customer_dates', 'value' =>$cd, 'compare' => '=');
}
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
$cm_metaq
),
array(
'relation' => 'OR',
$cd_metaq
),
)
)
);
My Wordpress setup has a Publication custom post type and has 10 custom fields each corresponding to an author who may have contributed to that publication. Every post can have different authors in those fields or the same authors but in a different order. I am using Wordpress's WP_Query to search for posts matching my criteria. I need to check a single author name against all the custom fields to see if it matches any one of them. I tried doing this with the below code but it produces no result. Any help would be appreciated!
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'key' => array(
'author_0_name',
'author_1_name',
'author_2_name',
'author_3_name',
'author_4_name',
'author_5_name',
'author_6_name',
'author_7_name',
'author_8_name',
'author_9_name',
),
'value' => $selected_pub_author,
'compare' => '=',
),
),
);
// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";
// The Query
$the_query = new WP_Query($args);
I have added OR relation for post author query,Try to change your argument array as follow,
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'author_0_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_1_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_2_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_3_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_4_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_5_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_6_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_7_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_8_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_9_name',
'value' => $selected_pub_author,
'compare' => '=',
),
),
),
);
$the_query = new WP_Query( $args );
I have a custom post type 'events' that contains custom fields for start_date and end_date. I'm trying to make a simple list of upcoming events. If I query only events with a start_date >= today it works fine. If I add in a meta_query to say "AND" all events with an end_date <= today - it doesn't return anything.
My custom fields (start_date, end_date) are stored as unix timestamps, which is why I'm using 'NUMERIC' and 'meta_value_num'. $today is a timestamp of the current date. Here are examples of what I'm trying to do... Pulling my hair out - any help would be appreciated!
THIS WORKS:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
);
When I add in a second array for the meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
When I use meta_compare in the main query, and a single meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'date',
),
)
);
When I use meta_compare in the main query, and multiple meta_queries - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
This is how I'm using it and its working fine for me.
$the_query = new WP_Query(array(
'post_type' => 'job',
'posts_per_page' => 25,
'meta_query' => array(
array(
'key' => 'published_date',
'value' => array('20140401','20140405'),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
)
));
if ($the_query->have_posts()) {
echo '<ul>';
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
echo 'Sorry! No Posts';
}
wp_reset_postdata();
The most important note on the codex.
The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
Here's what I ended up doing:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $yesterday,
'meta_compare' => '<=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => array($first_minute,$last_minute),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
)
This gets the current month's events. I input variables for first minute of month, last minute of month, and "yesterday" which is 11:59:59 of yesterday.
Not sure what are you trying but seems logical to not return anything as you are selecting events that starts today or in the future (start_date >= $today) AND ends today or in the past (end_date <= $today)...
Alexandru-Florentin Popescu is right! Use your code another following:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
));
You just need change key 'compare' in both arrays