MySQL查询wordpress元数据

时间:2012-05-10 00:58:13

标签: mysql wordpress

我需要一些查询的帮助,该查询应该根据他们的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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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