Fail to compare dates in meta_query

This is the piece of code I use to get event posts whom date metadata is newer than today :

    <?php
        query_posts( array(
        'post_type' => 'concerts',
        'meta_key' => 'numericdate',
        'posts_per_page' => -1,
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'numericdate',
                'value' => date('dmY'),
                'compare' => '>=',
                'type' => 'date'
                 )
           )
    ) );
    if (have_posts()) : ?>

This returned no results.
The “numericdate” field contains a “ddmmyyyy” string, so I expected comparing with a “dmY” today date value will work. I was wrong. Or there is some mistake in the code above.
Thanks for your help !

Solutions Collecting From Web of "Fail to compare dates in meta_query"

I have exact the same problem here, except i use the std. post type (post) and a category in my query. everything works except the sort order.

$d = date("Y-m-d");
$args = array(
    'post_type'         => 'post',
    'category_name'     => 'events',
    'post_status'       => 'publish,draft,pending,future,private', // just for me
    'meta_key'          => 'event_start',
    'orderby'           => 'meta_value_num',
    'order'             => 'ASC',
    'posts_per_page'    => -1,
    'meta_query'        => array(
        array(
            'key' => 'event_start',
            'value' => $d,
            'type' => 'date',
            'compare' => '>'
        )
    )
);

But my posts get sorted by String-Comparison nonetheless. What did i do wrong?

Well after a little trial and error i found out that instead of ‘meta_value_num’ i should’ve used ‘meta_value’:

$d = date("Y-m-d");
$args = array(
    'post_type'             => 'post',
    'category_name'     => 'events',
    'post_status'           => 'publish,draft,pending,future,private',
    'meta_key'              => 'event_start',
    'orderby'               => 'meta_value',
    'order'                     => 'ASC',
    'posts_per_page'        => -1,
    'meta_query'            => array(
        array(
            'key' => 'event_start',
            'value' => $d,
            'type' => 'date',
            'compare' => '>'
        )
    )
);

For whats its worth, use wordpress current_time() for this sort of queries.

current_time('mysql') gives server time according to admin settings that is acceptable by MySQL

I believe MySQL date format should be YYYY-MM-DD. Also I think orderby should be meta_value_num, otherwise the values are treated as strings.

MySQL’s date format preferences aside, there are a lot of good reasons to use bigendian formats like Y-m-d.

You would think that comparing dates would be obvious, but…

05262011 > 01012012

and so on. You’ll cause yourself lots of problems trying to use dmY format.

Also, I don’t think that ‘date’ is one of the accepted parameters for meta_query “type”. Leave that parameter out and use string comparison for best results.

If someone has the same problem you better look at posts_clauses filter.
It allows you to manipulate the query SQL itself.
There you can convert the date column value and change the where clause and orderby clause.

$pieces['where'] .= " AND STR_TO_DATE(wp_postmeta.meta_value,'%d%m%Y' ) >= 
  CURRENT_DATE() ";

$pieces['orderby'] = "STR_TO_DATE( wp_postmeta.meta_value,'%d%m%Y' ) DESC";