如何通过mysql在wordpress中获取所有类别的所有帖子? [英] How to get all posts with all categories in wordpress through mysql?
问题描述
我正在尝试获取所有发布的且基于不同类别的帖子,但是我得到的是所有帖子都带有一个类别名称的帖子.我想要的是每个帖子及其所属的类别.我的代码是:
I am trying to get all the posts that are published and are based on different categories, but what I get is all the posts with one category name on all of them. What I want is each posts with the categories they belong to. My code is :
$query = mysql_query("
SELECT p.post_title
, t.slug
, t.name
, AVG(l.rating_rating) as average
, l.rating_posttitle
FROM wp_posts p
JOIN wp_ratings l ON l.rating_postid = p. ID
JOIN wp_term_relationships r ON r.object_id = p.ID
JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
JOIN wp_terms t ON t.term_id = x.term_id
WHERE post_type ='post'
AND x.taxonomy = 'category'
GROUP
BY p.post_title;
") or die(mysql_error);
while($row = mysql_fetch_assoc($query)){
$title = $row['rating_posttitle'];
$rating = $row['average'];
$category = $row['slug'];
echo "<br>";
echo $club_title . "<br>";
echo $club_rating . "<br>";
echo $club_category . "<br>";
echo "<br>";
echo "<br>";
}
我得到的是:
Boujis 4.6667 俱乐部评论
Boujis 4.6667 club-reviews
盒子, 4.5000 俱乐部评论
Box, The 4.5000 club-reviews
尽管如此,它们都属于不同的类别,但是我在每个$ title和$ rating下得到了俱乐部评论.任何帮助将不胜感激.
Eventhough, they both belong to different categories, but I get club-reviews under each $title, and $rating. Any help would be appreciated.
谢谢
推荐答案
您必须按要更改的每个项目分组. 如果我理解您的要求是正确的
You have to group by every item you want to differ. If i understand you correct you should do
GROUP BY t.slug,p.post_title
这篇关于如何通过mysql在wordpress中获取所有类别的所有帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!