Last active
September 23, 2019 12:58
-
-
Save avillegasn/c89720b210e98d949fe27128ffdb58f4 to your computer and use it in GitHub Desktop.
Detect and remove unused images in WordPress
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
SELECT | |
* | |
FROM | |
wp_posts i | |
WHERE | |
i.post_type = 'attachment' | |
AND | |
NOT EXISTS (SELECT * FROM wp_posts p WHERE p.ID = i.post_parent) | |
AND | |
NOT EXISTS (SELECT * FROM wp_postmeta pm WHERE pm.meta_key = '_thumbnail_id' AND pm.meta_value = i.ID) | |
AND | |
NOT EXISTS (SELECT * FROM wp_posts p WHERE p.post_type <> 'attachment' AND p.post_content LIKE CONCAT('%',i.guid,'%')) | |
AND | |
NOT EXISTS (SELECT * FROM wp_postmeta pm WHERE pm.meta_value LIKE CONCAT('%',i.guid,'%')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment