Hi guys,
I’m having a bit of a problem with my media library after duplicating a live website to my local dev environment.
All my images got multiple database entries (same paths, multiple ids). This way it grew from about 900 images to over 3000, which makes it hard to maintain.
Is there a good way to rebuild the media library?
The alternative I see is to solve the problem with SQL directly in the database – where the chellange is to keep the version of the image that’s used on my posts and pages.
Any input would be greatly appreciated!
https://preview.redd.it/ngkbma0zfyoc1.png?width=1023&format=png&auto=webp&s=3de417ed87377dbeeeef95983ca8c245f394c998
[ad_2]
I found an SQL query that might help me. I only know some basic SQL so I don’t know if this is a solid solution:
DELETE pm
FROM wp_postmeta pm
JOIN (
SELECT
MIN(meta_id) as keep_id,
meta_value
FROM
wp_postmeta
WHERE
meta_key = ‘_wp_attached_file’
GROUP BY
meta_value
HAVING
COUNT(*) > 1
) as subq ON pm.meta_value = subq.meta_value
WHERE
pm.meta_key = ‘_wp_attached_file’
AND pm.meta_id != subq.keep_id;
It’s not a duplicate – the same image is being used as the featured image on 3 different posts.