Unattaching unlinked images

As I understand default WP behavior:

When you upload an image to a post and save it (as a draft or published post), the image becomes “attached” to the post. This can be double checked by going to Media > Library > ‘Uploaded To’ column.

In order to mark an image as unattached to any post, it must be done manually by deleting the post. (Is there another way?)

My main question is: Is there a way for me to tell WordPress to mark images as “unattached” if they are not inserted or linked in any existing posts?

edit

To clarify:

  1. We upload 20 (or more) images to a post and insert them. These are all automatically attached by WordPress to the post. We want this to happen, because our theme template pulls all attached images per post.

  2. Before the post is published, it’s edited and some images are taken out and won’t be used again.

  3. We’re looking for a way to detect which images are not being used* in the post, and automatically unattach them from that post. This is so that we can just head into Library > Unattached, select them all and delete them.

* I originally used the term ‘unlinked’ because all the inserted images are <a href=""><img></a> so it may help in terms of detecting what are being used because a link to the image is present in the post.

2 Answers
2

wordpress sucks in keeping media<=>content relationships. Part of the problem is that by default all media are public once they are uploaded and you have no way to know where are they are being used. Just because an image is not referenced anymore in its original post doesn’t mean that it is not referenced at any other place (sidebar widget?) or maybe by some external site, therefor deleting an image just because it is unattached is a very dangerous idea that can lead to a very big fail situation.

For what you want I think it is better to use the actual server access log than the content. Get a process running that once a month goes over all the images and find those that didn’t have any hits, and go and delete them.

Leave a Comment