Support

Account

Home Forums General Issues Query posts where end and/or start date are in the past Reply To: Query posts where end and/or start date are in the past

  • Try this

    
    $wp_query = new WP_Query( array(
      'post_type'    => 'event',
      'meta_query'   => array(
        'relation'      => 'OR',
        // if start date and end date are < now
        // OR
        // if start date < now and end date is empty
        array(
          'relation'    => 'AND',
          // if start date < now and end date is emptye
          array(
            'key'         => 'acf_event_date_start',
            'value'       => date('Ymd'),
            'compare'     => '<',
            'type'        => 'date'
          ),
          array(
            'key'         => 'acf_event_date_end',
            'value'       => '',
            'compare'     => '='
          )
        ),
        array(
          'relation' => 'AND',
          // if start date and end date < now
          array(
            'key'         => 'acf_event_date_start',
            'value'       => date('Ymd'),
            'compare'     => '<',
            'type'        => 'date'
          ),
          array(
            'key'         => 'acf_event_date_end',
            'value'       => date('Ymd'),
            'compare'     => '<',
            'type'        => 'date'
          )
        )
      ),
      'orderby'     => 'meta_value_num',
      'order'       => 'DESC',
      'paged'       => $paged
    ));