I want to query Wordpress data stored in a MySQL database, in order to get a result with columns:
- post_id
- category
- comma-separated tags
Expected output:
+---------------+----------+----------------+
| post_id | category | tags |
|---------------+----------+----------------+
| 213 | news | tag1,tag2,tag3 |
+---------------+----------+----------------+
Here's what I have tried:
SELECT
p.id,
c.name,
GROUP_CONCAT(t.`name`)
FROM wp_posts p
JOIN wp_term_relationships cr
on (p.`id`=cr.`object_id`)
JOIN wp_term_taxonomy ct
on (ct.`term_taxonomy_id`=cr.`term_taxonomy_id` and ct.`taxonomy`='category')
JOIN wp_terms c
on (ct.`term_id`=c.`term_id`)
JOIN wp_term_relationships tr
on (p.`id`=tr.`object_id`)
JOIN wp_term_taxonomy tt
on (tt.`term_taxonomy_id`=tr.`term_taxonomy_id`
and tt.`taxonomy`='post_tag')
JOIN wp_terms t
on (tt.`term_id`=t.`term_id`)
As a result, I get the columns I want, with the expected content, but I only get one row.
What am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…