1

我需要一些帮助,查询应该根据他们的 wp-postratings 分数返回帖子 (http://wordpress.org/extend/plugins/wp-postratings/)。

用户选择最低评分(0 到 5 星)和最高评分(0 到 5 星),查询应返回匹配的帖子。我让它在两个值的用户输入都高于 0 的情况下工作,但我似乎无法理解 0 值。由于 0 表示未评级的帖子 - 因此也有一次没有评级元数据 - 我不仅需要选择评级不超过指定最大值的帖子,还需要选择每个没有评级元数据的帖子。

我怎样才能做到这一点??任何帮助将不胜感激!

这是我当前的查询:

SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score 
FROM wp_posts p 
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
INNER JOIN wp_terms t ON  t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID 
LEFT JOIN wp_postmeta AS t2 ON t1.post_id = t2.post_id 
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID 
WHERE t1.meta_key = 'ratings_average' 
AND t2.meta_key = 'ratings_users' 
AND t3.meta_key = 'ratings_score' 
AND p.post_date < NOW() 
AND p.post_status = 'publish' 
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
AND ( (t1.meta_value+0.00) IS NULL OR (t1.meta_value+0.00) <= $max_stars )
ORDER BY p.post_date DESC 
LIMIT 20
4

2 回答 2

1

不久前,我不得不做类似的事情,当时我正在运行一个 cron 作业,以将帖子发送到另一个尚未注册的应用程序。我发现最好的方法是编写一个查询,检查 ID 是否不在使用元键的帖子查询中。

SELECT $wpdb->posts.ID
FROM $wpdb->posts 
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.ID NOT IN (
    SELECT $wpdb->posts.ID
    FROM $wpdb->posts
    left join $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'meta_key')

我相信这应该可行,尽管我显然没有测试过。

SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID
LEFT JOIN wp_postmeta AS t2 ON t1.post_id = t2.post_id
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID
WHERE t1.meta_key = 'ratings_average'
AND t2.meta_key = 'ratings_users'
AND t3.meta_key = 'ratings_score'
AND p.post_date < NOW()
AND p.post_status = 'publish'
AND (tt.taxonomy = 'post_tag' 
AND tt.term_id = t.term_id 
AND t.slug = 'liverpool')
AND ( 
p.ID NOT IN (
    SELECT p.ID 
    FROM wp_posts AS p
    LEFT JOIN wp_postmeta AS pm ON (pm.post_id = p.ID)
    WHERE pm.meta_key = 'ratings_score'
)
OR 
(t1.meta_value+0.00) <= $max_stars )
ORDER BY p.post_date DESC
LIMIT 20
于 2012-05-10T07:19:07.243 回答
1

好的,这个查询似乎对我有用。虽然它有点难看而且不是太快,所以如果有人有更好的,请随时改进它!

它选择所有低于 $max_stars 值的已评分帖子,然后将该表与一个单独的选择组合起来,该选择会获取所有未评分的帖子:

(SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score 
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
INNER JOIN wp_terms t ON  t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID 
LEFT JOIN wp_postmeta AS t2 ON t2.post_id = p.ID 
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID 
WHERE t1.meta_key = 'ratings_average' 
AND t2.meta_key = 'ratings_users' 
AND t3.meta_key = 'ratings_score' 
AND p.post_date < NOW() 
AND p.post_status = 'publish' 
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
AND (t1.meta_value+0.00) <= $max_stars )

UNION

(SELECT DISTINCT p.*, NULL AS ratings_average, NULL AS ratings_users, NULL AS ratings_score 
FROM wp_posts p 
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id) 
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) 
INNER JOIN wp_terms t ON  t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON (t1.post_id = p.ID AND t1.meta_key = 'ratings_score')
WHERE t1.post_id is null
AND p.post_date < NOW() 
AND p.post_status = 'publish' 
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool') )

ORDER BY post_date DESC
于 2012-05-10T21:14:35.777 回答