What are WordPress orphan relationships and how to clean them?

Sometimes the ‘wp_term_relationships’ table becomes bloated with many orphaned relationships. This happens particularly often if you’re using your site not as a blog but as some other type of content site where posts are deleted periodically. Over time, you could get thousands of term relationships for posts that no longer exist which consumes a lot of database space.
To clean these orphan relationships, you can either use our plugin Advanced Database Cleaner, or execute the following sql code (which is used by the plugin) directly via your phpMyAdmin:

DELETE FROM term_relationships WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM posts)

Leave a Reply

Your email address will not be published. Required fields are marked *