WPDB SQL忽略`POST_STATUS`参数

时间:2015-04-09 作者:skywind

我使用以下SQL语句:

SELECT * FROM wp_posts
    LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
    LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
    WHERE wp_posts.post_type = \'post\'
        AND wp_posts.post_status = \'publish\'
        AND wp_posts.post_status != \'draft\'
        AND wp_term_taxonomy.term_id = 17
        OR wp_term_taxonomy.term_id = 18
        OR wp_term_taxonomy.term_id = 19
    ORDER BY post_date_gmt DESC 
    LIMIT 6 
    OFFSET 6
但结果显示post_status = draft.

为什么会这样?

1 个回复
最合适的回答,由SO网友:websupporter 整理而成

首先,你用where post_status = \'publish\'. 因此,您无需通过使用wp_posts.post_status != \'draft\'

我认为,您的SQL表示如下内容:

抓取所有已发布、未起草并附于term\\u id 17的帖子OR 抓取所有附加到term\\u id 18的帖子OR 抓取所有附在term\\u id 19上的帖子。

所以你应该用括号来表示

抓取所有已发布并附加到term\\u id 17或term\\u id 18或term\\u id 19的帖子

尝试以下操作:

SELECT 
    * 
FROM 
    wp_posts 
    LEFT JOIN 
    wp_term_relationships ON (
        wp_posts.ID = wp_term_relationships.object_id
    ) 
    LEFT JOIN 
    wp_term_taxonomy ON (
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    ) 
WHERE (
    wp_posts.post_type = \'post\' AND 
    wp_posts.post_status=\'publish\' AND 
    (
        wp_term_taxonomy.term_id = 17 OR wp_term_taxonomy.term_id = 18 OR wp_term_taxonomy.term_id = 19 
    )
)
ORDER BY 
    post_date_gmt DESC 
    LIMIT 6 
    OFFSET 6

结束