Support

Account

Home Forums General Issues Time sorting for time picker field not working regardless of format

Solving

Time sorting for time picker field not working regardless of format

  • Hi, I’m having trouble getting posts to sort by a custom ‘start_time’ field, created with the time picker plugin and using format “h:mm tt” (though it fails to sort when I change this to “HH:mm” as well. My hope was that, where sorting failed with 12 hour format, I could enter times in 24 hour and just return a formatted date on the front-end. The query just doesn’t seem to sort regardless. What it does is sort the times by ascending numerical value (e.g. 6:00 PM before 12:00 PM).

    Here’s the query I’m running:

    <?php $args = array(
    	'post_type' => 'friday_event',
    	'orderby' => 'meta_value_num',
    	'meta_key' => 'start_time',
    	'order' => 'ASC',
    	'nopaging' => true
    );
    query_posts( $args );
    while ( have_posts() ) : the_post(); ?>

    Any idea what I’m doing wrong or why the posts just don’t seem to sort regardless of format? Thanks!

  • I’ve also tried using ‘orderby’ => ‘meta_value’ and a combination of meta_query arrays where ‘key’ => ‘start_time’ and I still haven’t gotten the posts to sort correctly.

    In fact, where it seemed at first to be doing doing a numerical sort and ignoring the UNIX timestamp format, I’m querying three other post types that use identical custom fields and it looks like there’s really no sorting going on at all, with events coming in any order (3, 2, 5 and 6pm, for example, in one of the post archives).

    Thanks!

  • Hi @seankmchenry

    To sort posts via date value, the format MUST be in “yymmdd” (Javascript date format) as described in this doc:
    http://www.advancedcustomfields.com/resources/field-types/date-picker/

    After you make that change and re-save the posts, your sort will work.

    Thanks
    E

  • Hi Elliot,

    For some reason I set the save format as: YY_mm_dd a long time ago, and now I need to sort posts by that value.
    I’m afraid that if I simply change the save format to yymmdd now, I’ll lost 100+ posts’ date value…
    What would you advise? Should I change the save format and then correct the dates already stored in the database somehow? They are saved as: 2011_06_23.

    Thanks,
    elba

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

The topic ‘Time sorting for time picker field not working regardless of format’ is closed to new replies.