Last active
October 5, 2015 18:25
-
-
Save ksemel/e39e12cc8c7823a59043 to your computer and use it in GitHub Desktop.
Delete WordPress Orphans in post_meta and taxonomy tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Delete Orphaned Post Meta | |
# Selects all postmeta where the post_id does not exist | |
SELECT * FROM wp_postmeta WHERE post_id NOT IN (SELECT DISTINCT ID FROM wp_posts) AND meta_id BETWEEN 1 AND 500000; | |
# Use an offset to avoid super massive queries! | |
# Make sure to offset the META_ID ( not the post_ID ) or you'll delete good data | |
# Set 1 | |
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT DISTINCT ID FROM wp_posts) AND meta_id BETWEEN 1 AND 500000; | |
# Set 2 | |
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT DISTINCT ID FROM wp_posts) AND meta_id BETWEEN 500000 AND 1000000; | |
# Set 3 | |
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT DISTINCT ID FROM wp_posts) AND meta_id BETWEEN 1000000 AND 2000000; | |
# Set 4 | |
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT DISTINCT ID FROM wp_posts) AND meta_id > 2000000; | |
# etc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How Many Orphans | |
#SELECT COUNT(*) FROM wp_term_relationships WHERE object_id NOT IN (SELECT DISTINCT ID FROM wp_posts); | |
# What are they exactly? | |
SELECT COUNT(object_id) as post_count, wp_term_taxonomy.taxonomy, wp_terms.name FROM wp_term_relationships | |
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id | |
JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id | |
WHERE object_id NOT IN (SELECT ID FROM wp_posts) | |
GROUP BY wp_term_relationships.term_taxonomy_id | |
ORDER BY post_count DESC; | |
# Verify and drop | |
DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT DISTINCT ID FROM wp_posts); # 26805 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Count | |
#SELECT COUNT(ID) FROM wp_posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 30 day) AND ( post_type = 'revision' OR post_name LIKE '%-autosave' ); | |
#SELECT COUNT(meta_id) FROM wp_postmeta WHERE post_id NOT IN ( SELECT ID FROM wp_posts ); | |
# Remove | |
DELETE FROM wp_posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 30 day) AND ( post_type = 'revision' OR post_name LIKE '%-autosave' ); | |
DELETE FROM wp_postmeta WHERE post_id NOT IN ( SELECT ID FROM wp_posts ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment