Simple WP Query upcoming events with start OR end date

I’m working with an upcoming events list. I create a custom post type, with a custom field: event_start_date. I also have some multiday extension events, defined by an additional custom field: event_end_date .

I have a code that work fine for events with a start_date later than today. But I fail incorporating the end_date condition. I try with a meta_query, with OR relation, that currently don’t work:

Read More
$args = array(
    'post_type' => 'events',
    'posts_per_page' => 4,
    'orderby' => 'event_start_date',
    'order' => 'ASC',
    'meta_query' => array(
         'relation' => 'OR',
          array('key'  => 'event_start_date',
              'value' => date('m/d/Y', strtotime('-1 day')),
              'compare' => '>='),

              array('key'  => 'event_end_date',
                  'value' => date('m/d/Y', strtotime('-1 day')),
                  'compare' => '>=')
    )
);
$loop = new WP_Query( $args ); 
while ( $loop->have_posts() ) : $loop->the_post();
// Rest of the loop

Thanks in advance.

Note: I have this other query, that works perfect selecting events with only a start date > than current date.

                    $args = array(
                    'post_type' => 'events',
                    'posts_per_page' => 4,
                    'meta_key'  => 'event_start_date',
                    'meta_value' => date('m/d/Y', strtotime('-1 day')),
                    'meta_compare' => '>=', 
                    'orderby' => 'event_start_date',
                    'order' => 'ASC',           
                    ); // The same loop and query

Related posts

Leave a Reply

2 comments

  1. Actually to order by a meta key the meta-key must be present in the query and use the meta_value as orderby value.

    $args = array(
        'post_type' => 'events',
        'posts_per_page' => 4,
        'meta_key' => 'event_start_date', // required to be used in orderby
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'key'  => 'event_start_date',
                    'value' => date('m/d/Y'),
                    'compare' => '>'
                ),
                array(
                    'key'  => 'event_end_date',
                    'value' => date('m/d/Y'),
                    'compare' => '>'
                )
        ),
        'orderby' => 'meta_value', // orderby will use the value of meta_key
        'order' => 'asc',
    );
    $loop = new WP_Query($args);
    

    Also you can use a type in your each array of meta_query as follows

    array(
        'key'  => 'event_end_date',
        'value' => date('m/d/Y'),
        'compare' => '>',
        'type' => 'DATE' // to specify the type of the field/value
    )
    

    Update: You should use date('m/d/Y') and 'compare' => '>' and make sure your date format is correct because right now it’s in (month/day/year). date('m/d/Y', strtotime('-1 day')) will show past dates too.

    This should work if everything else is correct.
    Read Here.

  2. Solution not worked for me (mybe topic is too old for older versions).
    For next searcher – solution to show all past events (with pagination)

      'post_type' => 'event', 
      'suppress_filters' => false,
      'meta_key'  => '_eventorganiser_schedule_last_start', // OR _eventorganiser_schedule_start_start OR ..._start_finish OR ..._last_finish
      'meta_value' => date('Y-m-d', strtotime('today')),
      'meta_compare' => '<', 
      'orderby' => '_eventorganiser_schedule_last_start',
      'order' => 'DESC',
      'paged' => get_query_var('paged')