Last active
March 18, 2022 08:11
-
-
Save moritzbappert/a47f8879b3c649565b2ad80292d35b5e to your computer and use it in GitHub Desktop.
Delete unnecessary media duplicates created by WPML (with EN as source language)
This file contains hidden or 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 FROM | |
wp_posts | |
WHERE | |
post_type = 'attachment' | |
AND ID NOT IN( | |
SELECT | |
MIN(ID) | |
FROM ( | |
SELECT | |
ID, post_type, post_name FROM wp_posts) AS r1 | |
JOIN wp_icl_translations ON (ID = element_id) | |
WHERE | |
post_type = 'attachment' | |
AND language_code = 'en' | |
GROUP BY | |
post_name | |
) | |
AND ID IN( | |
SELECT | |
ID FROM ( | |
SELECT | |
ID, post_type FROM wp_posts) AS r2 | |
JOIN wp_icl_translations ON (ID = element_id) | |
WHERE | |
post_type = 'attachment' | |
AND language_code = 'en' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this to clean up my messed-up WordPress database, where WPML created several duplicates for the same images in the database.
Make sure to change 'en' to your source language and always back up your database before using commands like this.