Delete all WordPress posts in a category via MySQL

Delete all WordPress posts in a category via MySQL

Quick note on deleting all posts in a category in a WordPress website.

Review

This will select the items that will be removed, but not remove them. Good for reviewing your work before actually deleting.

Replace the X in the last line with your category ID.

SELECT *
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 )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = X

Delete

This will delete data! Replace X in the last line with your category ID.

delete a,b,c,d
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 )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = X;

Leave a Comment