Skip to content

Instantly share code, notes, and snippets.

@chambaz
Last active August 29, 2015 13:56
Show Gist options
  • Save chambaz/8848770 to your computer and use it in GitHub Desktop.
Save chambaz/8848770 to your computer and use it in GitHub Desktop.
Query Wordpress for posts with associated meta data
SELECT p.ID,
p.post_title,
p.post_date,
p.post_excerpt,
p.post_content,
MAX(CASE WHEN pm.meta_key = '_aioseop_description' then pm.meta_value ELSE NULL END) as meta_description,
MAX(CASE WHEN pm.meta_key = '_aioseop_title' then pm.meta_value ELSE NULL END) as meta_title
FROM wp_posts p LEFT JOIN wp_postmeta pm ON ( pm.post_id = p.ID)
WHERE p.post_status = 'publish' AND p.post_type = 'post'
GROUP BY p.ID, p.post_title;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment