Skip to content

Instantly share code, notes, and snippets.

@moritzbappert
Last active March 18, 2022 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritzbappert/a47f8879b3c649565b2ad80292d35b5e to your computer and use it in GitHub Desktop.
Save moritzbappert/a47f8879b3c649565b2ad80292d35b5e to your computer and use it in GitHub Desktop.
Delete unnecessary media duplicates created by WPML (with EN as source language)
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'
);
@moritzbappert
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment