SQL query delete post and postmeta by meta key and value

To delete all posts by a specific meta key and optionally meta value I run the following SQL query:

DELETE p, pm
  FROM wp_posts p
  JOIN wp_postmeta pm
    ON pm.post_id = p.ID
 WHERE pm.meta_key = 'meta_key_name'
   AND pm.meta_value = 'some value';

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.


    No comments yet..

Leave a reply

Feel free to ask questions or make comments.