自定义$wpdb返回基于时间的意外结果

时间:2013-03-26 作者:Roman Epicnerd Sharf

我正在使用$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
        )

)

1 个回复
最合适的回答,由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)";

结束

相关推荐

执行自定义SQL查询时使用格式打印页面内容

我想这有一个简单的解决方案,但找不到任何解决方案。我正在试着取3页,并在页脚上显示它们。问题是every page is shown as text without formatting 也就是说shortcodes are printed as text as well.下面是我使用的代码:global $wpdb; $sql = \"SELECT post_title, post_content FROM ...\"; $footerElements = $wpd