
After an intensive use of your WordPress blog (creating posts, editing posts, and deleting posts) your database will contain thousands of rows of useless meta data. Here is a simple SQL query to delete orphaned post meta on your database.

After an intensive use of your WordPress blog (creating posts, editing posts, and deleting posts) your database will contain thousands of rows of useless meta data. Here is a simple SQL query to delete orphaned post meta on your database.
Just run the following query on your WordPress database to delete orphaned post meta. Don't forget to replace the table prefix wp_ if your database is using another prefix.
And of course, create a backup of your database before running the query!
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL
Thanks to Patrick Rauland for submitting this useful recipe!
2 Responses
That is perfect! Thanks so much, I was looking to do this recently on over 2000 posts, something I was going to go back to when I had more time; This just helps me get on with it without having to figure it out myself
thanks again
Use the plugin “Optimize Database”. It will compact your database daily to keep lots of “orphaned” junk from building up in there.
It also empties your “trash” directories and truncates the mile-long Revisions list for your frequently updated pages.
Trackbacks: