Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save loorlab/43e98bda31621c8b83fbbb5800a47783 to your computer and use it in GitHub Desktop.
Save loorlab/43e98bda31621c8b83fbbb5800a47783 to your computer and use it in GitHub Desktop.
Query - MySQL - WordPress | Display all Posts with Categories exclude pages and other content type
SELECT
p.ID,
p.post_title,
p.post_status,
CONCAT(u.option_value, '/', p.post_name, '/') AS permalink,
GROUP_CONCAT(t.name ORDER BY t.name SEPARATOR ', ') AS categories
FROM
wp_posts p
JOIN
wp_options u
ON u.option_name = 'siteurl'
LEFT JOIN
wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN
wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'category'
LEFT JOIN
wp_terms t ON tt.term_id = t.term_id
WHERE
p.post_type = 'post'
AND p.post_status IN ('publish', 'draft', 'pending', 'private')
GROUP BY
p.ID, p.post_title, p.post_status, permalink
ORDER BY
p.post_date DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment