I’m looking for a safe and fast way to delete all of one custom post type’s posts. Using get_posts()
and wp_delete_post()
for each returned post does not work; it’s not fast enough due to the sheer amount of database queries involved (timeout error).
Preferably, I’m looking for a single database query to run that deletes all posts that are of a custom post type. Any thoughts?
You can delete all post via $wpdb
DELETE FROM wp_posts WHERE post_type="post_type";
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);
DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts)
or use this query replace it with {{your CPT}} with your Custom Post Type
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b
ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c
ON (a.ID = c.post_id)
WHERE a.post_type="{{your CPT}}";