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

  • Wow thanks, that works perfectly!
    I ended up doing this:

    /* Ongoing Past Events */
    $ongoing = new WP_Query( array(
      'post_type' 	 => 'event',
    	'meta_query'   => array(
      	'relation'      => 'AND',
        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'
    ));
    if ($ongoing->have_posts()) :
      $exclude = array();
      while($ongoing->have_posts()) : $ongoing->the_post();
        $exclude[] = $post->ID;       
      endwhile;
    endif;
    
    /* Past Events */
    $paged    = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query = new WP_Query( array(
    	'post_type' 	 => 'event',
    	'post__not_in' => $exclude, // Exclude ongoing past events
    	'meta_query'   => array(
        array(
          'key'         => 'acf_event_date_start',
          'value'       => date('Ymd'),
          'compare'     => '<',
          'type'        => 'date'
        )
      ),
    	'orderby' 		=> 'meta_value_num',
    	'order'       => 'DESC',
    	'paged' 			=> $paged
    ));

    Which also works, but you need 2 queries which is not ideal.
    Thanks for your help!