I’m trying to remove duplicate images from my wordpress media library.
The posts themselves aren’t duplicate but each attachment image is appearing twice for each post.
I’ve had a look around but nobody quite has a clear cut answer. Some people have said use something like this:
<?php
$p = get_posts(array('numberposts'=> -1));
foreach($p as $t) {
$s = get_children(array('post_type' => 'attachment', 'numberposts' => -1 ));
foreach ($s as $u) {
var_dump($u);
}
}
?>
But there still seems to be a bit missing, like this brings me a list of attachments but I still don’t know how to compare them.
It seems to me that I need to use some sql queries and delete the media files straight from the database. I’m not quite sure how to go about this though.
Theoretically I need to attempt something like find posts, foreach post get attachments, if attachment filename == filename then delete filename.
Help appreciated.
Create a file in your theme folder and in that paste either the first or second way solutions:
1st way (Compare against their titles – recommended):
2nd way (Compare against their urls)
The way it works is, it checks each file against each other. So if you have a file like file.jpg and file1.jpg, then it will catch them as duplicates. Also, if you have a file like file1.jpg and file11.jpg, then it will catch them as duplicates even though they might be completely different files.
:
Then try to access that file in your browser and it will show you a list of all the duplicates.