Sometimes when developing plugins or testing things on a WordPress website, you may end up some ghost or orphaned postmeta – and no posts.

This will delete all postmeta, where the post does not exist any more.

  1. DELETE FROM `wp_postmeta` WHERE wp_postmeta.post_id NOT IN (SELECT id FROM `wp_posts`)

If you need to delete the post and the postmeta via phpMyAdmin, you could use the following: Delete the postmeta first, then the post by post type or whatever your conditions may be.  In this example I have chosen to delete posts and postmeta if its post_type is a product.

  1. # Deletes ALL postmeta WHERE the post_type IS a product
  2. DELETE FROM `wp_postmeta` WHERE wp_postmeta.post_id IN (SELECT id FROM `wp_posts` WHERE post_type = 'product')
  3.  
  4. # This THEN deletes ALL posts WHERE post_type IS a product
  5. DELETE FROM `wp_posts` WHERE post_type = 'product'

Leave a Reply

Your email address will not be published. Required fields are marked *