How to order by just the Time Field in Advanced Custom Fields

I’m using Advanced Custom Fields to create a class schedule. I need to get the time field to sort correctly to display the class schedule in the correct order. Currently it is not, and I don’t understand what I need to do to get it sorting correctly.

Here is the live link: http://studio34yoga.com/yoga-movement/class-schedule/ You can see on Monday that the classes are not in the correct order. “start_time” is my time picker field that I am hoping to sort on. What follows is an abridged version of my PHP code. Any help is appreciated.

<?php

$args = array( 'post_type' => 'Classes', 'posts_per_page' => 70, 'meta_key' => 'start_time', 'orderby' => 'meta_value_num', 'order'=>'ASC' );       
$loop = new WP_Query( $args );


while ( $loop->have_posts() ) : $loop->the_post();

if(get_field('day') == "2monday") :

{
    echo '<div class="class"><span class="class-time">';
    echo get_field('start_time');
    echo '&nbsp;-&nbsp;';
    echo get_field('end_time');
    echo '</span></div>';
}

endif;

endwhile;

?>

Related posts

1 comment

  1. It looks like the times are being stored as plain text strings like “10:00 am – 12:00 pm” which will be pretty impossible to sort correctly during your wp_query call.

    Try the date and timepicker add-on and it should allow you to store in a sortable format. You can use it for only times and not dates. Then if you need a time range, you can have “start_time” and “end_time” and then sort by “start_time”.

Comments are closed.