我已使用“高级自定义字段”插件创建了自定义字段。此字段称为状态,默认情况下等于即将发生,然后稍后将更改为正确或错误。
然后我尝试查询状态元值等于正确或错误的所有帖子。但是,即使我在下面的查询中设置了它,它仍会返回所有帖子的计数。我已经仔细检查并计算了有多少帖子等于正确或错误,但仍然得到错误的计数。我做错了什么?
SELECT count(DISTINCT posts.ID) as count, AVG(odds.meta_value) as ave
FROM $wpdb->posts posts
LEFT JOIN $wpdb->postmeta stat
ON posts.ID = stat.post_id
AND stat.meta_key = 'status'
LEFT JOIN $wpdb->postmeta odds
ON posts.ID = odds.post_id
AND odds.meta_key = 'odds'
LEFT JOIN $wpdb->term_relationships tr ON posts.ID = tr.object_id
LEFT JOIN $wpdb->term_taxonomy t ON tr.term_taxonomy_id = t.term_taxonomy_id
WHERE stat.meta_value = 'wrong'
OR stat.meta_value = 'correct'
AND posts.post_status = 'publish'
AND t.taxonomy = 'category'
AND t.term_id = 4
OR t.term_id = 5
OR t.term_id = 6
答案 0 :(得分:0)
您应该使用括号将应该在一起的内容组合在一起:
SELECT count(DISTINCT posts.ID) as count, AVG(odds.meta_value) as ave
FROM $wpdb->posts posts
LEFT JOIN $wpdb->postmeta stat
ON posts.ID = stat.post_id
AND stat.meta_key = 'status'
LEFT JOIN $wpdb->postmeta odds
ON posts.ID = odds.post_id
AND odds.meta_key = 'odds'
LEFT JOIN $wpdb->term_relationships tr
ON posts.ID = tr.object_id
LEFT JOIN $wpdb->term_taxonomy t
ON tr.term_taxonomy_id = t.term_taxonomy_id
WHERE (stat.meta_value = 'wrong'
OR stat.meta_value = 'correct')
AND posts.post_status = 'publish'
AND t.taxonomy = 'category'
AND (t.term_id = 4
OR t.term_id = 5
OR t.term_id = 6)