Support

Account

Home Forums General Issues Sort CPT with multiple dates Reply To: Sort CPT with multiple dates

  • Okay, so if you have multiple datepicker fields on a Course, try something like this on your single-course.php to only show dates that are in the future:

    $today = date( 'Y-m-d' );
    if(get_field('date_1') > $today):
      echo '<p>' . get_field('date_1') . '</p>';
    endif;
    
    if(get_field('date_2') > $today):
      echo '<p>' . get_field('date_2') . '</p>';
    endif;
    
    etc...

    Again, just make sure that the date format for $today matches what you specified in your datepicker field in the ACF settings. If that doesn’t work, you may need to do a php strtotime() with the dates.

    Querying Courses by multiple ACF field dates would be a little trickery.

    $today = date( 'Y-m-d' );
    $args = array(
    	'post_type'  => 'course',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key'     => 'date_1',
    			'value'   => $today,
    			'compare' => '>',
    		),
                    array(
    			'key'     => 'date_2',
    			'value'   => $today,
    			'compare' => '>',
    		),
    	),
    );
    $query = new WP_Query( $args );

    I just don’t think your can order a WP Query by multiple fields.