简体   繁体   中英

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>";    

    }

What I get is:

Boujis 4.6667 club-reviews

Box, The 4.5000 club-reviews

Eventhough, they both belong to different categories, but I get club-reviews under each $title, and $rating. Any help would be appreciated.

Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM