WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

current order:


01/01/2013 should appear last

My code:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$today = date('m/d/Y');
    'post_type' => 'post',
    'category_name' => 'flavors',
    'paged' => $paged,
    'meta_key' => 'flavor_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
         'meta_query' => array(
        'key' => 'flavor_date',
        'value' => date("m/d/Y", strtotime('-6 hours')),
        'compare' => '>=',
        'type' => 'CHAR'
if (have_posts()) :
while (have_posts()) : the_post();

Function Used:

add_filter('posts_join', 'new_join' );
function new_join($pjoin){
        global $wpdb;
        $pjoin .= "LEFT JOIN (
FROM $wpdb->postmeta
WHERE meta_key =  'flavor_date' ) AS metasort
ON $wpdb->posts.ID = metasort.post_id";
    return ($pjoin);

 add_filter('posts_orderby', 'new_order' );
function new_order( $orderby ){
    global $wpdb;
        $orderby = "metasort.meta_value ASC";

    return $orderby;
share|improve this question

The only date format that sorts into human calendar order is "year/month/day". The separators shouldn't matter. If you left your dates in UNIX time format, as you get from strtotime, they would sort appropriately as well.

I would recommend re-organizing your code to match one or the other of those formats. I would go further and recommend that you use unix time. If you save those values in UNIX time format, you can easily display them however you want. That is, just use date("m/d/Y", $unixtimeformatedstring) when you display instead of when you save.

I know unixtime is hard to read, but it plus the PHP functions that can manipulate it, is a miracle worker when it comes to dealing with dates (as long as you don't need dates too far in the past :) )

share|improve this answer

Your Answer


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.