0
votes

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.

1

1 Answers

1
votes

In your archive query, you are using two meta_query conditions, I think the issue is with the first condition

First condition: event_start_date

array(
    'key'     => 'event_start_date',
    'type'    => 'DATE',
    'value'   => current_time('Ymd'),
    'compare' => '<',
),

This will query all events if the event_start_date is less than current_time. Also consider that you are creating an OR relation.

Now, let's say today is Oct 10 and an even spans from Oct 9 to Oct 12, the above meta_query will include this event as well because the event_start_date is less than current_time. Make sense?

The solution will be to include meta_query for event_end_date only. This way, only events with ending date less than current_time will be queried.

/* 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(
         array(
            'key'     => 'event_end_date',
            'type'    => 'DATE',
            'value'   => current_time('Ymd'),
            'compare' => '<',
        ),
    ),
);

Update: Nested meta_query logic to handle single day past events as well.

/* 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' => 'AND',
        array(
            'key'     => 'event_start_date',
            'type'    => 'DATE',
            'value'   => current_time('Ymd'),
            'compare' => '<',
        ),
        array(
            'relation' => 'OR',
            array(
                'key'     => 'event_end_date',
                'type'    => 'DATE',
                'value'   => current_time('Ymd'),
                'compare' => '<',
            ),
            array(
                'key'     => 'event_end_date',
                'type'    => 'DATE',
                'value'   => NULL,
                'compare' => '=',
            ),
        )
    ),
);