Support

Account

Home Forums General Issues Ordering/Filtering wp_query using ACF Date field Reply To: Ordering/Filtering wp_query using ACF Date field

  • Giving it a little more thought, this should handle where the end date is not set, I think.

    
    <?php
    $recent_args = array(
      'post_type'  => 'event',
      'meta_query' => array(
        'relation' => 'AND',
        array(
          'relation' => 'OR',
          array(
            'key'     => 'end_date',
            'value'   => $date_2,
            'compare' => '<',
          ),
          array(
            'key'     => 'end_date',
            'compare' => 'NOT EXISTS',
          ),
        ),
        array(
          'relation' => 'OR',
          array(
            'key'    => 'end_date',
            'compare'   => 'BETWEEN',
            'type'     => 'DATE',
            'value'   => array($date_1, $date_2),
          ),
          // if no end date has been set use start date
          array(
          'key'       => 'date',
          'compare'   => 'BETWEEN',
          'type'     => 'DATE',
          'value'   => array($date_1, $date_2),
          ),
        ),
      ),
      'orderby'     => 'meta_value_num',
      'order'          => 'DESC',
      'nopaging'       => true
    );
    ?>