Home » Wordpress » Mysql delete all posts that have a given meta_key [duplicate]

Mysql delete all posts that have a given meta_key [duplicate]

Posted by: admin February 26, 2018 Leave a comment

Questions:

This question already has an answer here:

Answers:

Using Mysql query you can use join in delete query

DELETE p.*,w.*
FROM `wp_postmeta` w
JOIN wp_posts p
 ON(p.ID = w.`post_id`)
WHERE w.`meta_key` LIKE 'acest_meta'

Above query will delete all posts and its meta data contains a meta_key as acest_meta

Questions:
Answers:

you can delete with a select using an IN() statement and put the select in an IN like so

DELETE FROM `wp_postmeta` 
WHERE `post_id` IN 
(   SELECT `post_id`
    FROM `wp_postmeta`
    WHERE `meta_key` LIKE 'acest_meta' 
)

Questions:
Answers:
delete from wp_postmeta
 where post_id in
       (SELECT post_id FROM wp_postmeta WHERE meta_key = 'acest_meta')