为了能够比较日期,您应该使用 Y/m/d 格式(或 Ymd ...等)。
使用您的参数,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
所以 :
- 您可以修改数据库中的日期值
- 或者您可以构建自己的查询以在比较之前转换这些值