我正在使用$wpdb类进行自定义SQL查询,但得到的结果很奇怪。基本上这就是我的SQL查询应该做的
只有在过去7天内发布的帖子类型“question”的标题具有“question\\u tag”分类,才可检索这些标题。问题是数字2。查询似乎返回的问题日期晚于7天。为了说明这一点,我还检索了post\\u日期。正如您在下面的结果集中所看到的。题为“lkddfs”的问题已经提出7天多了。帮助
function rs_send_email_to_interested_users() {
global $wpdb;
$wpdb->show_errors();
$sql ="SELECT DISTINCT $wpdb->posts.post_title, $wpdb->posts.post_date FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE 1=1 AND
$wpdb->term_taxonomy.taxonomy = \'question_tag\' AND
$wpdb->posts.post_type = \'question\' AND
$wpdb->posts.post_status = \'publish\' AND
$wpdb->terms.slug = \'another-sample\' OR $wpdb->terms.slug = \'sample\' AND
//the date logic doesn\'t seem to qork quite right
$wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";
$question_titles = $wpdb->get_results( $sql, ARRAY_N );
return $question_titles;
}
下面是我的结果:
Array
(
[0] => Array
(
[0] => lkddfs
[1] => 2013-03-03 03:05:57 // This question is more than 7 days old!
)
[1] => Array
(
[0] => Good tactical games
[1] => 2013-03-24 04:48:04
)
[2] => Array
(
[0] => my recent question
[1] => 2013-03-24 14:58:25
)
[3] => Array
(
[0] => romans question
[1] => 2013-03-26 21:18:00
)
)
最合适的回答,由SO网友:vancoder 整理而成
这可能是你的问题:
$wpdb->terms.slug = \'another-sample\' OR $wpdb->terms.slug = \'sample\' AND
//the date logic doesn\'t seem to qork quite right
$wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";
OR运算符可能会混淆查询。尝试使用括号:
($wpdb->terms.slug = \'another-sample\' OR $wpdb->terms.slug = \'sample\') AND
//the date logic doesn\'t seem to qork quite right
$wpdb->posts.post_date > DATE_SUB( NOW(), INTERVAL 7 DAY)";