I need an SQL to clean my wordpress database by post_title.
For example, the following finds all posts from wordpress that contain the word "apple" in the title:
SELECT * FROM wp_posts WHERE post_title LIKE '%apples%';
And this deletes those same posts:
DELETE FROM wp_posts WHERE post_title LIKE '%apples%';
However, Im not sure how to delete all references in other tables by title was well because I don't know how they relate to the wp_posts table. I THINK you can remove rows related to posts from postmeta via the following AFTER you remove posts containing "apple":
DELETE FROM wp_postmeta WHERE NOT EXISTS (SELECT * FROM wp_posts WHERE wp_postmeta.post_id = wp_posts.ID)
This should work because after the initial SQL command, you now have rows in wp_postmeta without a relationship to the wp_posts table and removes said rows. Simple.
But I'm not sure how to remove the category and tag references because Im not sure of the relationships between wp_posts and wp_terms, wp_term_relationships or wp_term_taxonomy (in addition to any other table that could be effected by the first query).
Can someone help me here?
Thanks for all consideration.
post_id
again? Although, if the tables have certain types of referential integrity enabled, removing the posts may remove all the related rows automatically (ieON DELETE CASCADE
). – Clockwork-MuseUPDATE
s orDELETE
s over multiple tables (ie - with aUNION
). You're going to need to write some sort of script or stored procedure, supposing you can run it. Otherwise, just run them one-at-a-time... – Clockwork-Muse