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

Handy mysql query to get all posts with a missing 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  
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
  AND wp_postmeta.meta_key = 'CUSTOM_FIELD_NAME'
WHERE wp_postmeta.meta_key is null and wp_posts.post_type = 'page'
order by wp_posts.ID asc

Note that we used a left join with two conditions.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments