Support

Account

Home Forums General Issues Sorting by 2 acf fields, date and time problem Reply To: Sorting by 2 acf fields, date and time problem

  • Ok I sorted it out myself. You need to use named meta query.

    <?php // Meta query for tab
    	$current_date = date('Ymd');
    	$future_date = date('Ymd', strtotime('+6 days'));
    
    	$args = [
    	    'post_type' => 'events',
    	    'post_status' => 'publish',
    	    'posts_per_page' => 12,
    	    'meta_query' => array(
            'relation' => 'AND',
    	        'date_meta' => array(
    	           	'key' => 'event_date',
    	            'value' => [$current_date, $future_date], // Use an array to specify a range
    	            'compare' => 'BETWEEN', // Use BETWEEN to specify a range
    	            'type' => 'DATE',
    	        ),
    	        'time_meta' => array(
    	            'key' => 'start_at', // 'start_at' is the custom field storing the time value
    	            'type' => 'TIME' // Use 'TIME' meta type for 'start_at' field
    	        ),
    	    ),
    	    'orderby' => [
    	        'date_meta' => 'ASC', // Order by 'event_date' in ascending order
    	        'time_meta' => 'ASC' // Then order by 'start_at' in ascending order
    	    ]
    	];
    
    	include(locate_template('template-parts/tabs-inner-include.php', false, false));
    ?>