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.

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 !

share|improve this question

4 Answers 4

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

share|improve this answer

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.

share|improve this answer
1  
Indeed, the CAST() operation will return NULL if your data is not in the correct format. It also accepts YYYYMMDD without the separators, but not DDMMYYYY. I don't know about the orderby - would it use the casted values of the meta query? –  Jan Fabry May 27 '11 at 13:44
    
Thanks Milo, it now works like a charm ! meta_value_num did the job right, I'm wondering why no one noticed such a detail in this similar thread : bit.ly/l88vup –  Pierre May 28 '11 at 11:57

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' => '>'
        )
    )
);
share|improve this answer

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.

share|improve this answer
    
date is valid, from WP_Query entry in codex: Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'. –  Milo May 27 '11 at 1:26
    
I stand corrected. Thanks. –  goldenapples May 27 '11 at 16:43

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.