Skip to content

Instantly share code, notes, and snippets.

@nathan-roberts
Last active August 5, 2022 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-roberts/57f9d30e017f32acef0949e9a0859743 to your computer and use it in GitHub Desktop.
Save nathan-roberts/57f9d30e017f32acef0949e9a0859743 to your computer and use it in GitHub Desktop.
WP SQL Meta Query
SELECT
wp_posts.ID,
wp_posts.post_title,
bill.meta_value AS bill,
year.meta_value AS year,
chapter.meta_value AS chapter,
house.meta_value AS house
FROM
wp_posts
LEFT JOIN wp_postmeta AS bill ON wp_posts.ID = bill.post_id
AND bill.meta_key = 'bill'
LEFT JOIN wp_postmeta AS year ON wp_posts.ID = year.post_id
AND year.meta_key = 'year'
LEFT JOIN wp_postmeta AS chapter ON wp_posts.ID = chapter.post_id
AND chapter.meta_key = 'chapter'
LEFT JOIN wp_postmeta AS house ON wp_posts.ID = house.post_id
AND house.meta_key = 'house'
WHERE
wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
ORDER BY
wp_posts.post_title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment