Perform query with meta_value date

I did something similar, the technique you need to use is called a meta query.

Here is the query I wrote to get posts based on a date value stored as a custom field meta value.

query_posts(
array(
'post_type'=>'post',
'order'=>'ASC',
'orderby'=>'meta_value_num',
'meta_key'=>'date_event',
'posts_per_page'=> -1,
'meta_query' => array(
    'relation' => 'AND',
       array(
           'key' => 'date_event',
           'value' => $startDate, // Lowest date value
           'compare' => '>='
       ),
       array(
           'key' => 'date_event',
           'value' => $endDate, // Highest date value
           'compare' => '<='
       )
   )
));

'orderby' => 'meta_value_num' will sort the results by the meta value

'meta_key' => 'date_event' tells what the custom field is called

meta_query is an array which specifies that the results must match both conditions, then we specify a minimum date and a maximum date.

The date values must be stored either as YYYY-MM-DD or as a “Seconds since the Unix Epoch” value.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)