0

I'm working on a site that has pages for both "Upcoming" and "Archived" events for the "event" custom post type. I set up custom fields (via ACF) for event_start_date and event_end_date when entering each event, and while I was able to configure the right meta_query for the "Upcoming" page, I'm having an issue with the "Archived" page specifically in the case that no end date is entered, i.e. a single-day event in the past. Thus, if possible, I'd like the query to account for (1) when both the start date and end dates are in the past; and (2) when only a start date is entered that's in the past. Here is my query as it exists now:

$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' => 'NOT EXISTS',
                ),
            )        
        ),
    );

For reference, my meta_query for "Upcoming" events is as follows, and is working fine in cases of single dates and ranges in the future:

'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, how might I adjust the first meta_query for archived events with only a start date entered? Thank you for any assistance here. UPDATED adjusted meta_query with NOT EXISTS per comment below; but still not working as intended.

  • 1
    Does the event_end_date key actually exist for those posts, or should the comparison be NOT EXISTS? – Milo Oct 13 '17 at 15:42
  • @Milo that's correct, in this case the event_end_date does not exist; I just tried updating the meta_query with NOT EXISTS as you suggested (see updated question above), and it's still not working in the case that no end date is entered- any ideas? (However, if the same date is entered for start and end and it occurs in the past, it will show up in the archive; I'd like to avoid requiring that step if possible.) – nickpish Oct 13 '17 at 15:58
  • It might be helpful to look at $your_wp_query->request after the query is run to see the SQL being sent to the database, just to verify it's what you expect it to be. Another option, which would result in a more efficient query, would be to hook post save and automatically copy event_start_date to event_end_date in the case where it doesn't exist, so you can eliminate that check from the query and also not require the user to manually enter a date. – Milo Oct 14 '17 at 0:48
  • @Milo- thanks for the response; could you elaborate on how I'd automatically copy event_start_date to event_end_date in cases that the latter doesn't exist? That seems like a great approach and would indeed simplify things across the board. – nickpish Oct 14 '17 at 17:55
  • 1
    There are some examples on the save_post action codex page. I know ACF has its own actions that trigger whenever posts are saved or updated, you might want to look around in their docs and support forums for that. – Milo Oct 14 '17 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.