Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect and remove unused images in WordPress
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
You can’t perform that action at this time.