Skip to content

Instantly share code, notes, and snippets.

@avinash
Created March 16, 2023 07:36
Show Gist options
  • Save avinash/be8feb3dca6847a75ab3fa4f03f6f0f3 to your computer and use it in GitHub Desktop.
Save avinash/be8feb3dca6847a75ab3fa4f03f6f0f3 to your computer and use it in GitHub Desktop.
Export WordPress posts in a more digestible format
-- See https://stackoverflow.com/a/13265356 and the subsequent comments
SELECT DISTINCT
post_title
, post_content
,(SELECT DISTINCT wpm2.meta_value FROM wp_posts wp INNER JOIN wp_postmeta wpm ON (wp.ID = wpm.post_id AND wpm.meta_key = '_thumbnail_id') INNER JOIN wp_postmeta wpm2 ON (wpm.meta_value = wpm2.post_id AND wpm2.meta_key = '_wp_attached_file') WHERE wp.ID = wp_posts.ID) AS "Featured Image"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'category' and wp_posts.ID = wpr.object_id
) AS "Categories"
,(SELECT group_concat(wp_terms.name separator ', ')
FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy= 'post_tag' and wp_posts.ID = wpr.object_id
) AS "Tags"
FROM wp_posts
WHERE post_type = 'post'
ORDER BY post_title, post_content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment