Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;

?>
share|improve this question
    
How are you storing the time? Are you using the date and time picker add on? advancedcustomfields.com/add-ons/date-time-picker Or manually entering it? And in what format? –  Jake Jul 16 '13 at 14:19
add comment

1 Answer 1

up vote 0 down vote accepted

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".

share|improve this answer
    
THanks for your help on this... I will look closer at it. I thought I was using the date / time picker, as it gives me a time chooser interface -- It is not just a text field. –  Samantha Slade Jul 16 '13 at 18:30
    
If you are using it, it should store in the database as a unix timestamp which is great for ordering... Let me know if you are using that, or if you are using a different add-on. Would curious to see why it is not working. –  Jake Jul 16 '13 at 18:53
    
Thanks, Jake. I have the date / time field installed and activated, which I am assuming is what would be giving me the field. I have no other addons / plugins that I would be using. Everything I read on this seems to talk about ordering by date, and it's not as helpful for me to understand how to order by time. –  Samantha Slade Jul 16 '13 at 19:38
    
I tried to test, but the date/time field doesn't work right for me... It saves a nice timestamp into the db, but it doesn't show in the metafield after. So all I can say is make sure that you have "Save as timestamp?" set to "Yes". –  Jake Jul 17 '13 at 21:02
1  
Thanks, Jake -- This resolved itself with the latest update to ACF and ACF date / time picker. You were so helpful for me to determine that it wasn't me but the plugin! :) –  Samantha Slade Aug 16 '13 at 10:49
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.