Skip to content

Instantly share code, notes, and snippets.

@hayakawa
Created April 19, 2017 01:36
Show Gist options
  • Save hayakawa/329571736bca2586f3bf2670d19cdd8c to your computer and use it in GitHub Desktop.
Save hayakawa/329571736bca2586f3bf2670d19cdd8c to your computer and use it in GitHub Desktop.
How to get post list with category from Wordpress
SELECT a.ID
, a.post_date
, a.post_modified
, a.post_title
, a.post_name
, b.user_registered
, b.user_login
, b.user_nicename
, b.display_name
, c.name
, c.slug
FROM wp_posts a
LEFT JOIN wp_users b
ON a.post_author = b.ID
LEFT JOIN (
SELECT sub_a.name
, sub_a.slug
, sub_c.object_id
FROM wp_terms sub_a
LEFT JOIN wp_term_taxonomy sub_b
ON sub_a.term_id = sub_b.term_id
LEFT JOIN wp_term_relationships sub_c
ON sub_b.term_taxonomy_id = sub_c.term_taxonomy_id
WHERE sub_b.taxonomy = 'category'
) c
ON a.ID = c.object_id
WHERE a.post_status = 'publish'
AND a.post_type = 'post'
ORDER BY a.post_date ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment