Compare WP Custom Field date

I had create a custom field “date” with this format 16/09/2013 (d/m/Y).

How can i compare date and show only post with date from today ?

This is my query that don’t work !!

$args = array(  
'posts_per_page' => 100,    
    'meta_key' => 'date',
    'orderby' => 'meta_value',
    'order' => 'ASC',   
    'meta_query' => array(
    array(
        'key' => 'city',
        'value' => 'London',
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'date',
        'value' => date("Y/m/d"),
'compare' => '>=',
    'type' => 'DATE'
    ),
)
);

1 Answer
1

Take a look at meta.php:777:

... CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})...

So if you want to use DATE comparisons, you should use MySQL compatible date formats (YYYY-MM-DD).

Change this part of your code:

array(
    'key' => 'date',
    'value' => date("Y/m/d"),
    'compare' => '>=',
    'type' => 'DATE'
),

to:

array(
    'key' => 'date',
    'value' => date("Y-m-d"),  // <- change
    'compare' => '>=',
    'type' => 'DATE'
),

and it should work just fine.

Leave a Comment