WordPress mysql query to get all posts with a specific custom field

Handy mysql query to get all posts with a specific custom field. We’ll query post_type=page for the purpose of this tutorial. You will have to replace CUSTOM_FIELD_NAME with the real custom field name.

SELECT wp_posts.ID, wp_postmeta.meta_key
FROM wp_posts  
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
  AND wp_postmeta.meta_key = 'CUSTOM_FIELD_NAME'
WHERE wp_posts.post_type = 'page'
order by wp_posts.ID asc
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments