您好,我正试图显示今天的所有活动。它是一个自定义字段,因此它存储在postmeta表中。当我尝试比较日期时,它应该返回没有结果。我正在匹配正确的日期格式。
$today = date('d/m/Y',time());
$args = array(
'post_type' => 'live',
'meta_query' => array(
array(
'key' => 'date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE',
)
)
);
任何帮助,我将不胜感激。
由于
答案 0 :(得分:1)
为了能够比较日期,你应该使用Y / m / d格式(或Y-m-d ......等)。
使用你的args,SQL查询将是:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'live' AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'date' AND CAST(wp_postmeta.meta_value AS DATE) >= '17/04/2012') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
哪个错了,例如18/03/2012 >= 17/04/2012
是真的......
如果您使用Y / m / d格式并遵循args:
$args = array(
'post_type' => 'live',
'meta_query' => array(
array(
'key' => 'date',
'value' => date('Y/m/d',time()),
'compare' => '>=',
)
)
);
它将按预期工作:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'live' AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'date' AND CAST(wp_postmeta.meta_value AS CHAR) >= '2012/04/17') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
所以: