I'm using a custom fields plugin for WordPress (Advanced Custom Fields) and am using a time picker field set to format: "h:mm tt". Using a typical WP query, I'm attempting to sort by "start time" in ascending order, but the returned lists of posts seems to sort on ascending numerical value (i.e. 12:00 PM would appear after 6:00 PM). I checked the custom field data and it's definitely being saved as a UNIX timestamp (start time of '1383224400' for example).

I've passed a few combinations of arguments to query_posts but nothing I've tried has worked. Here is the query I'm using:

<?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 ideas what I might be doing wrong? I've tried using a custom 'meta_query' where I set 'meta_value' to a variable and do a custom compare, but I still can't get the posts to sort correctly. Any help is very much appreciated!


I don't see any reason why your code should not work, and when I test things it does work. I think that one of the following is happening:

  1. Your meta_key is wrong. Perhaps it is prepended by a _ for example.
  2. You don't have correct, or consistent, timestamps
  3. You have sticky posts getting in the way
  4. There is a filter acting on the query.

To solve #3, add 'ignore_sticky_posts' => true, to the arguments. For the others, I can't say. I don't the ability to investigate those possibilities.

| improve this answer | |

I had the same problem two years ago but, I got a tip to get my custom metafields sorted correctly.
first you must define the orderby field, for example you has written "meta_value_num" but which is the value_num? or which field has the value or values to orderby?
For example I have to sort for a price value

$args = array(
'post_type' => 'post',
'posts_per_page' => '10',
'paged' => get_query_var( 'paged' ),
'key' => 'price_value',
'value' => 'price_value',
'type' => 'numeric',
'orderby' => 'price_value',
'order'   => 'ASC'

query_posts( $args );

?php if (have_posts()) : ?>
?php while (have_posts()) : the_post(); ?>

where price_value is the meta field that WP will look into, and also is the meta value number to be sorted. All data can be sorted, even if data are unformatted.

| improve this answer | |
  • You should review the Codex page for WP_Query. About half of your arguments are wrong/used incorrectly. This won't work as written. – s_ha_dum Nov 5 '13 at 18:48
  • @s_ha_dum the code is working since two years ago on 10 sites, I had to remove some < and <?php because the lines it wasn´t shown properly. – Fernando Baltazar Nov 5 '13 at 19:08
  • Use the same arguments to create a new WP_Query, then var_dump the object. You can see in the SQL that this does not do what you intend. Most of those arguments are simply ignored. – s_ha_dum Nov 5 '13 at 19:18
  • The same is true if you just dump $wp_query after your query_posts runs. – s_ha_dum Nov 5 '13 at 19:32
  • Can this be done with just 'orderby' => 'meta_value_num', 'meta_key' => 'start_time', 'meta_value' => '', somehow setting the 'meta_value' key to the time value stored in the 'start_time' key? The fields are saved in DB as UNIX timestamps, so I imagine sorting on this field should be rather straightforward. – Sean M. Nov 5 '13 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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