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
Related
I'm trying to set up a wordpress page in which there is a main section with upcoming events and a secondary section in which there are passed events.
I'm using this filter (located in functions.php) in order to switch upcoming and passed events:
// upcoming events
function listing_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '>=',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('listing_filter', 'listing_filter_function');
// events archive
function archive_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('archive_filter', 'archive_filter_function');
In this filter the events are sorted using the start date, but I would also need to check the end date to prevent events lasting more than one day from being show in the archive section after the first day
Thanks in advance!
you can use the meta_query relation for this like this example
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>='
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
)
for the archive you can check if the end_date is smaller then today or equal or less (<=)
Edit to answer the Question in the comment, this was asked after marking the answer correct. Please note I'm not 100% on this as I'm readying mixed results about this method so if it works please let us know so I can update this to reflect the results
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
),
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '1',
'compare' => 'NOT EXISTS'
),
),
),
it finally works!I've made some changes:
'meta_query'=> array('relation' => 'OR',
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '',
'compare' => '='
),
),
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
),
),
I want to order or group all the posts with meta_value with same meta_key.
All posts with meta_value 'CCC' and with 'AAA' and with 'BBB',...
I try with this :
'meta_query' => array(
array(
'key'=>'a_post_meta_key',
'value'=> array('CCC','AAA','BBB','DDD')
)
),
'orderby' => 'meta_value',
'order' => 'DESC',
And this :
'meta_query' => array(
'relation' => 'OR',
'ccc_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'CCC',
'compare' => '='
),
'aaa_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'AAA',
'compare' => '='
),
'bbb_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'BBB',
'compare' => '='
),
),
'orderby' => array(
'ccc_clause' => 'DESC',
'aaa_clause' => 'DESC',
'bbb_clause' => 'DESC',
),
But no good results. Do you have any ideas ?
Thank you
When I use the acf plugin is normal, but when using acf pro plugin does not working ???
$wp_query_post = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' =>
array(
array(
'key' => 'timestart',
'compare' => '<=',
'meta_type' => 'DATE',
'value' => $datetime,
),
'relation' => 'AND',
array(
'key' => 'timeend',
'compare' => '>=',
'meta_type' => 'DATE',
'value' => $datetime,
)
)
)
);
Convert strtotime and try like below,
$wp_query_post = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' =>
array(
array(
'key' => 'timestart',
'compare' => '<=',
'meta_type' => 'DATE',
'value' => intval(strtotime($datetime)),
),
'relation' => 'AND',
array(
'key' => 'timeend',
'compare' => '>=',
'meta_type' => 'DATE',
'value' => intval(strtotime($datetime)),
)
)
)
);
Probably have done this already, but make sure the format of $datetime matches the format of the custom fields, timestart and timeend.
You may also need to change meta_type, depending on the time format:
'meta_type' => 'DATETIME'
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/
I am trying to query posts using custom meta fields (date) and order them by the custom meta field (start_date).
I need to look for posts that start on or after todays date or end after today's date.
It all seems to work apart from the order by start date and OR statement, any help appreciated.
<?php
$today = date('y-m-d');
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>'
)
)
);
query_posts( $args );
?>
I am assuming you are MySQL database for wordpress and date format should be YYYYMMDD.
Update date format: use $today = date('Y-m-d'); in stead of $today = date('y-m-d');
Also type=>date in meta_query like below:
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'type' => 'date',
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'type' => 'date',
'compare' => '>'
)
)
);
Hope will help!