Skip to content

Instantly share code, notes, and snippets.

@qutek
Created May 7, 2015 06:39
Show Gist options
  • Save qutek/84c6242a27d3cbbf983c to your computer and use it in GitHub Desktop.
Save qutek/84c6242a27d3cbbf983c to your computer and use it in GitHub Desktop.
[Wordpress][Database] Display meta_value as column
SELECT p.ID, p.post_type, p.post_status, m.* FROM wp_posts p LEFT JOIN
( SELECT
post_id,
GROUP_CONCAT(if(meta_key = 'campaign_description', meta_value, NULL)) AS 'description',
GROUP_CONCAT(if(meta_key = 'campaign_start', meta_value, NULL)) AS 'start',
GROUP_CONCAT(if(meta_key = 'campaign_end', meta_value, NULL)) AS 'end',
GROUP_CONCAT(if(meta_key = 'campaign_priority', meta_value, NULL)) AS 'priority'
FROM wp_postmeta
GROUP BY post_id ) m
ON m.post_id = p.ID
WHERE p.post_type = 'campaign' AND p.post_status = 'publish'
AND m.start = '05/07/2015'
AND m.end = '05/20/2015'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment