Skip to content

Instantly share code, notes, and snippets.

@vpnwall-services
Created October 27, 2020 22:35
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 vpnwall-services/8f300373121cf4ddcac922e1a2acee26 to your computer and use it in GitHub Desktop.
Save vpnwall-services/8f300373121cf4ddcac922e1a2acee26 to your computer and use it in GitHub Desktop.
[MYSQL Find orphaned images] Mysql find orphaned images #mysql #find #orphaned #images
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