Important Warning and Preparation
Before directly operating on the WordPress database, always perform a complete backup. Incorrect SQL commands can lead to permanent data loss. For backup methods, refer to: WordPress Database Backup Tutorial.
Procedure
Step 1: Confirm Target Data
Before executing the delete operation, it is strongly recommended to run a query to confirm the posts to be deleted are exactly the ones you expect. Replace 128 in the following SQL statement with the term taxonomy ID of the category whose posts you want to delete.
SELECT *
FROM `wp_posts`, `wp_term_relationships`
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = 128;
Explanation: This query lists all posts (and their details) associated with the specified category ID (term_taxonomy_id). Review the results carefully.
Step 2: Execute the Delete Operation
After confirming the data is correct, you can execute the delete command. Again, replace 128 with your target category ID.
DELETE `wp_posts`
FROM `wp_posts`, `wp_term_relationships`
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = 128;
Command Analysis: This SQL statement deletes all post records from the wp_posts table that are associated with the specified term_taxonomy_id in the wp_term_relationships table.
Important Notes and Additional Information
- Finding the ID: The category's "term taxonomy ID" can usually be found in the WordPress admin under Posts -> Categories. Hover over the category name and check the link in your browser's status bar for the
tag_IDparameter. - Associated Data: This operation only deletes the posts themselves. Associated data like attachments, custom fields (Post Meta), etc., may require additional cleanup. A more thorough approach is to use a WordPress plugin or call the
wp_delete_post()function within a deletion loop. - Alternative Methods: For non-technical users, it is safer to use a "Bulk Actions" plugin or manually filter by category in the WordPress admin and move posts to the trash in bulk.
- Table Prefix: If your WordPress database uses a custom table prefix (not
wp_), replace the table names in the SQL statements (e.g.,wp_posts) with your actual prefix (e.g.,myprefix_posts).