Compare the current date to a timestamp in WordPress with meta_query

I would like to make a query to compare the current date to a date in the database. This way I would show events that happened before today.

I tried the meta_query but all events are shown.

Read More
 'meta_query' => array(
         array(
             'key'     => 'event_start_date_number',
             'value'   => date('Y-m-d'),
             'compare' => '<=',
             'type' => 'DATETIME,'
         ),
    ),

For reference in my database I got many records that looks like this:

meta_key: event_start_date_number
meta_value: 1472576400

And they was saved this way:

$date = "".$event_start_date." ".$event_start_time."";
$event_start_date_number = strtotime($date);
if(!empty($event_start_date_number)) {
    update_post_meta($post_id, 'event_start_date_number', $event_start_date_number);
}

Related posts

1 comment

  1. convert the meta_value to date format for comparison

    date and strtotime

    'meta_query' => array(
         array(
             'key'     => 'event_start_date_number',
             'value'   => date("Y-m-d", $event_start_date_number),
             'compare' => '<=',
             'type' => 'DATETIME',       
         ),
    ),
    

Comments are closed.