Support

Account

Home Forums Front-end Issues WP Query – Order by number and then time

Solved

WP Query – Order by number and then time

  • I have a CPT called classes and you I am looking to order the classes by number (e.g. 1,2,3 etc.) and then by time (e.g. 9:am, 11am, 8pm etc.)

    I can get the number order but not by time and not sure where I’m going wrong;

    $args = array( 
    	'post_type' => 'classes', 
    	'posts_per_page' => $number_of_classes,
    	'post_status' => 'publish',
    	'meta_query' => array(
    		'relation' => 'AND',
    		'date_clause' => array(
    			'key' => 'date',
    			'compare'	=> '=',
    		),
    		'time_clause' => array(
    			'key' => 'from_time',
    			'compare'	=> '=',
    		),
    	),
    	'orderby' => array(
    		'date_clause' => 'ASC',
    		'time_clause' => 'ASC',
    	),
    );

    Any ideas or help would be great, thanks.

  • How are you storing the values? I’ve previously found storing a date or time in a text field has caused some issues. Are they dedicated date/time fields?

  • Thanks for commenting @jarvis.

    The number field is a select field which is working fine in the query.

    The time field uses the ‘Time Picker’ field.

  • Do you need to declare the type?

    'time_clause' => array(
    	'key'		=> 'from_time',
    	'compare'	=> '=',
    	'type'		=> 'time',
    ),

    What does the time field return? Is it HH:MM:SS?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.