How To Use SQL To Delete All Posts In A WordPress Category

This handy script allows you to delete all posts and all their related content from your WordPress installation…PERMANENTLY. Please use it with CAUTION and always perform backups! Use this script only at your own risk.

How To Use SQL To Delete All Posts In A WordPress Category

Step 1:

First you need to log into your phpMyAdmin and select your WordPress database.

Step 2:

From the horizontal menu, select the “SQL” tab and execute the following SQL command. This only selects posts, so this is a SAFE command to run. Make sure you change YOURCATEGORYID to the category ID that you want.

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

If the posts that are selected look fine, then you can run the following SQL command. As with the above, make sure you change YOURCATEGORYID to the category ID that you want. Warning again: This will PERMANENTLY DELETE all the selected posts!

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

Credits go to Lars for this tip!

, , , ,

About Site Fixit!

Sam is a professional web designer and web developer. He has over 15 years of experience with web-related technologies, and loves making things work.

View all posts by Site Fixit!

No comments yet.

Leave a Reply