I'm working on pages in a WordPress site to show "Upcoming" and "Archived" events by date range using custom fields for event_start_date
and event_end_date
with the custom post type event
. I'd like the "Upcoming" page to show posts for which the current date falls within this range and/or equals the start date or end date; and the "Archive" page to show events for which the current date is after the date range entirely. Thus, if today is October 10, and an event with the dates October 9-11 or October 10-12 would show on "Upcoming" and an event with any previous dates would show on the "Archived" page.
I'm including my queries below for both the upcoming and archive pages; the query for upcoming seems to be working ok, but in the archive page if the current date falls within the date range it will still show, and I'd like it to only show past events:
/* upcoming events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '>=',
),
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '>=',
),
),
);
/* archived events query */
$event_args = array(
'post_type' => 'event',
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_start_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
array(
'key' => 'event_end_date',
'type' => 'DATE',
'value' => current_time('Ymd'),
'compare' => '<',
),
),
);
Thus, the meta_query
for the upcoming events query seems to be working for all cases, but that for the archive page is not behaving as intended. Thank you for any assistance here, and please let me know if my question is unclear in any way. EDIT I initially neglected to mention that I also need to account for the scenario in which only a start date is entered, i.e. for single-day events, which is proving to be a sticking point for the archive page query.