Support

Account

Home Forums Backend Issues (wp-admin) Nest date field queries to handle optional end date in Event feed

Unread

Nest date field queries to handle optional end date in Event feed

  • I have a query for a past event feed which is ignoring my usual technique for creating the following behaviour:

    – If event (CPT) has an end_date value, check it is in the past, OR
    – If event does not have an end_date value, use the event_date value (required field) and check this is in the past

    I’ve done this several times by nesting the two clauses in an OR relation, but in this case this isn’t working. Any event with an event_date value in the past is turning up in the “Past Events” query results, even when an end_date value is present and in the future.

    Here’s the full query – it differs from my usual approach because it needs to filter out more options: event_type (radio button) and include_in_past_events (true/false).

    date_default_timezone_set('Europe/London'); 
    $past = date('Ymd', strtotime("-999 months")); 
    $now = date('Ymd', strtotime("now")); 
    
    $past_args = array(
     'post_type' => 'event',
     'meta_query' => array(
      'relation' => 'AND',
       array(
        'relation' => 'AND',
        array(
         'relation' => 'OR',
         // check to see if end date has been set
         array(
          'key' => 'end_date',
          'compare' => 'BETWEEN',
          'type' => 'NUMERIC',
          'value' => array($past, $now),
         ),
         // if no end date has been set use event/start date
         array(
          'key' => 'event_date',
          'compare' => 'BETWEEN',
          'type' => 'NUMERIC',
          'value' => array($past, $now),
         )
        ),
        array(
         'key' => 'event_type',
         'value' => array('Singlar', 'Multi-Day', 'Series'),
        )
       ),
       array(
        'key' => 'include_in_past_events',
        'value' => '1',
       )
      ),
      'meta_key'	=> 'event_date',
      'orderby' => 'meta_value_num',
      'order' => 'DESC',
      'nopaging'	=> false,
      'posts_per_page'=> '8'
     );

    Any ideas why this isn’t working for me in this case?

    Thanks.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.