Support

Account

Home Forums General Issues Issue with query between 2 dates Reply To: Issue with query between 2 dates

  • This depends on what you’re looking for exactly.
    Do you have a range of dates that the start and end date of the event needs to be between? Do you have a date and anything after that date is shown? These would be completely different queries.
    For example, if I wanted all of the events with start or end dates after today, then I wouldn’t check the start date at all because if the end date is after today then the start date does not matter

    
    'meta_query' => array(
      array(
        'key' => 'end_date',
        'compare' => '>=',
        'value' => $today,
      )
    )
    

    But if I want events that are happening between two dates

    
    'meta_query' => array(
      'relation' => 'OR',
      array(
        array(
          'relation' => 'AND',
          array(
            'key' => 'start_date',
            'compare' => '>=',
            'value' => $start_range
          ),
          array(
            'key' => 'start_date',
            'compare' => '<=',
            'value' => $end_range
          )
        )
      ),
      array(
        array(
          'relation' => 'AND',
          array(
            'key' => 'end_date',
            'compare' => '>=',
            'value' => $start_range
          ),
          array(
            'key' => 'end_date',
            'compare' => '<=',
            'value' => $end_range
          )
        )
      )
    )