SQL PHPMyAdmin query to delete all posts in a WordPress Category


Some time ago you could use the category_name or cat keys in order to search for your posts using an SQL query at PHPmyAdmin. You cannot do this anymore since categories are replaced by taxonomy terms.

In the following example you see a query you can execute through PHPmyAdmin in order to delete all posts under a category. Just replace 2906 with the number of your own category. Be careful to make a backup of your database before. Never neglect that.

SQL query to delete all posts that belong to category 2906:

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 =2906

If you want to see how many posts this query will delete, you can use the following query, selecting the posts but without performing any action on them.

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 =2906