Created
May 3, 2019 17:15
-
-
Save edpittol/a753745d875f64c0af9231b4b17b4dc3 to your computer and use it in GitHub Desktop.
WP-CLI: Remove all media in the database that isn't on the filesystem.
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
wp db query "SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key='_wp_attached_file'" --skip-column-names | \ | |
paste -s -d '\n' - | \ | |
xargs -n 2 bash -c '\ | |
FILE=wp-content/uploads/$2; \ | |
[[ ! -f ${FILE} ]] && echo $1; \ | |
' argv0 | \ | |
sed -e 'H;${x;s/\n/,/g;s/^,//;p;};d' | | |
xargs -i bash -c 'wp db query " \ | |
DELETE p, pm, tr \ | |
FROM wp_posts p \ | |
LEFT JOIN wp_postmeta pm ON ( p.ID = pm.post_id ) \ | |
LEFT JOIN wp_term_relationships tr ON ( p.ID = tr.object_id ) \ | |
WHERE p.ID IN ({}) \ | |
"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment