dylanjameswagner
5/26/2016 - 3:06 PM

MySQL WordPress delete posts and postsmeta

MySQL WordPress delete posts and postsmeta

## select count of post_type
SELECT COUNT(*)
FROM `wp_posts`
WHERE `post_type` = 'post_type'

## delete posts of post_type
DELETE
FROM `wp_posts`
WHERE `post_type` = 'post_type'

## select count of orphaned postmeta
SELECT COUNT(*)
FROM `wp_postmeta` AS wp_postmeta
LEFT JOIN `wp_posts` AS wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.ID IS NULL

## delete orphaned postmeta
DELETE wp_postmeta
FROM `wp_postmeta` AS wp_postmeta
LEFT JOIN `wp_posts` AS wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.ID IS NULL