Compare the current date to a timestamp in Wordpre

2019-07-23 04:05发布

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.

 '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);
}

1条回答
该账号已被封号
2楼-- · 2019-07-23 04:46

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',       
     ),
),
查看更多
登录 后发表回答