danyo
danyo

Reputation: 5846

wordpress query posts by meta date value

I am trying to query a custom post type by a custom field that contains a date. My query looks like this:

query_posts(
    array(
    'showposts' => 500, 
    'post_type' =>  'holidays', 
    'post_status' => 'publish', 
    'order' => 'desc', 
    'orderby' => 'meta_value',
    'meta_query'   => array
                    (
                        array
                        (
                            'key'     => 'from_date',
                            'value'   => array( '01-01-2012', '31-12-2014' ),
                            'type'    => 'DATE', // TRIED: DATE, SIGNED, NUMBER
                            'compare' => 'BETWEEN'
                        )
                    ),



    )); 

My custom fields store the date like this: 19-11-2013

When i run the query no results are shown, although dates within the range exist.

Am i approaching this is the correct manor, or am i missing something?

Upvotes: 0

Views: 1632

Answers (1)

rhysclay
rhysclay

Reputation: 1683

I can confirm Dan White's answer is correct, when querying by a date range the required format needs to be 'YYYY-mm-dd'. Your final code should look like this:

query_posts(
    array(
    'showposts' => 500, 
    'post_type' =>  'holidays', 
    'post_status' => 'publish', 
    'order' => 'desc', 
    'orderby' => 'meta_value',
    'meta_query'   => array
                    (
                        array
                        (
                            'key'     => 'from_date',
                            'value'   => array( '2012-01-01', '2014-12-31' ),
                            'type'    => 'DATE', 
                            'compare' => 'BETWEEN'
                        )
                    ),

    )); 

Upvotes: 1

Related Questions