Support » Fixing WordPress » A lot of images reported as being unattached

  • Resolved trejder


    tl;dr: Some sites in my WordPress network reports a lot of images (even up to 70% of all images ever uploaded) as not being attached to any blog post. This is not true. In most sites all images are in fact used and in some there are truly only 2-3 images not used. How can I fix this? What to do in future to avoid this situation?


    While browsing through my Media Library I have noticed that a lot of images in it are reported as not being attached to any post:

    Upon closer investigation (displaying details of each image) it is confirmed, because each and every image displayed here does not have “Uploaded To” field filled-up in image’s details.

    However, this is 100% not true. Each and every image in above screenshot is actually attached to some post. Images from 2nd till 5th are added to one post in form of gallery and all remaining images (1st and 6th till 15th) are used as featured image for one or more posts in my blog.

    The same goes for each and every image listed with “Unattached” filter. All of them are in fact used as featured image. I don’t have even a single image that isn’t actually attached or used within at least one blog post, so I don’t know how contents of this screen are generated.

    I’ve been using my WordPress network and this particular site for 10+ years. I’ve been using featured images feature ever since WordPress and my theme started supporting them. And ever since then I’ve been adding featured images in exactly the same way — by using proper controls, first in classic editor then in Gutenberg.

    So each and every image in my blog is attached to some post, by the usual means, yet this looks like totally random. Some randomly selected images are reported as unattached.

    I have checked Media Library in my other sites, being part of the same WordPress network, and the situation is also very random. On one site I have zero images reported as unattached, on some other one I have just 2-3 reported as unattached and on one or two remaining I have more than 50-70% of all images reported as unattached.

    How can I fix this? What to do or what to look for when next time uploading image or setting featured image to avoid more and more images being mistakenly reported as unattached.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz


    Attachment is determined by the image’s attachment post type’s “post_parent” property. Unattached images have a parent value of 0. Attached images have the containing post’s ID for a value.

    Images edited through the media library’s image editor will frequently become unattached. Also, uploading images through Media -> Add New always show as unattached even when they are subsequently inserted into a post. Images inserted into a post, which are then also inserted elsewhere, will show as unattached if the original post is deleted even though the image still occurs in another post.

    I’m only aware of a fix which involves manually editing each attachment post type. Not a viable option if there are a lot of images to fix. The lack of attachment should have no ill effect on the front end. The only drawback I see is it’s difficult to locate the containing post for a given image if it remains unattached.

    Thread Starter trejder


    Seems like you have just solved my mystery!

    uploading images through Media -> Add New always show as unattached

    Does this mean that I should avoid this feature and rather switch back to the two step process of: (1) uploading image via Media and (2) adding only already previously uploaded images to the post?

    even when they are subsequently inserted into a post

    On the other hand, if I do exactly what is in your quote then the question is why after subsequently inserting image into a post it is not getting the relation to that post? Isn’t that some kind of bug? From how you have said this I am getting (maybe wrong) impression that you’re sounding like it would be a wanted feature.

    Moderator bcworkz


    If it’s very important for you to have image attachments reliably relate back to a post. then you should only insert images into posts through the post edit screen and always insert images from new uploads and not reuse existing images. Though you might consider giving some thought to why it’s important and if maybe it’s not really as important as it might seem.

    The image to post relationship has been only loosely enforced as far back as I know, roughly ten years. While it is possible to more strictly enforce relationships, it’s more cumbersome coding-wise and still not infallible. It has never been seen as all that important AFAIK. It’s maybe an artifact of WP origins as a blogging platform which typically involves a one way workflow. Following the expected workflow is what maintains the relationship. In a more flexible CMS workflow the scheme kind of falls apart.

    Could the scheme be improved upon? Of course, but I wouldn’t call it bug. A matter of semantics I suppose.

    Thread Starter trejder


    You should only insert images into posts through the post edit screen and always insert images from new uploads and not reuse existing images

    It seems that I have misunderstood your first post (or actually understood it exactly opposite). I thought (mistakenly) that I should always first upload images to Media and only then go to post editing and attach it to post. Upon reading your first answer again I see my mistake. I should do exactly opposite — always upload images directly to post.

    The thing is that reality in my case is as my first (incorrect?) understanding. From what I can see, nearly 99% of “orphan” (not attached) images are those that I have attached directly to post (via Featured Image > Set Featured Image > Upload New). And nearly 99% of images correctly attached to post were first uploaded to Media and then resused from there.

    The reality observations are quite exactly opposite to what you have written.

    Which makes me really puzzled.

    Moderator bcworkz


    No worries about misunderstanding. We all tend to comprehend what we expect to read regardless of what is said 🙂 If images freshly uploaded through post edit screen dialogs (featured or in content) fail to become attached to that post, then something is definitely abnormal. Can you reliably cause the issue to occur, or does it sometimes work OK? Intermittent errors are very difficult to diagnose.

    Ensure WP, your theme, and all active plugins are updated to the latest versions.

    If that does not help, switch your theme to twentytwenty and deactivate all plugins to eliminate all possible external conflicts. Images uploaded through the edit screen dialogs should become attached to the respective post. Restore your theme, then plugins, one at a time, testing after each. When the issue recurs, the last activated module is the cause.

    Thread Starter trejder


    First of all, I treat this entire “problem” more like research task, not a real problem, so we can end this discussion anytime, without any firm conlusions.

    I have everything up-to-date, as I update everything to the latest version 3-5 days after update is published at most.

    I don’t blog too often recently and I have images shown (in some of my blogs) as unattached even for these files / images that I have uploaded 6-12 months ago. So this is a persisting problem.

    I have just uploaded an image to my new blog post, exactly the way you told me to do (i.e. directly during post edit, not through Media) and that image become correctly attached / assigned to a post. Which is OK and expected, if I am getting you right.

    Here is this post:

    Neither images uploaded to this post nor any images in the blog are marked as unattached.

    But then, again, I’ve been doing this for many, many times previously (actually all my edits in past 6 months, in all 12 blogs) and I still have a lot of images shown as unattached.

    The thing is that for past 6-12 months I’ve been uploading images only directly then editing post (the correct way, as you said) and never through Media first (the incorrect way, as you said). And I have been uploading images the same way to all my blogs. Yet, some of the blogs (including above mentioned have 0 unattached images, while others have dozens of them.

    To summarise — method is the same for past a year and for all blogs, but results differs. So I think that there’s some deeper logic behind this problem, not just the method of how images are added to post. And I feel that we will never truly be able to find out what is going in here.

    Moderator bcworkz


    It’s sounding like one of those things like when you bring your car to the mechanic and the problem you’re having fails to present itself. It’s hard to fix something that does not appear to be broken. For many people, failure to be attached has little, if any, impact on site visitors. Unattached images behave the same as attached in most cases. While it’s annoying to see unattached images when you know they belong to a specific post, unless it’s materially affecting your workflow, it’s not something to be concerned about.

    Of course, you can always attach an unattached image after the fact, but it has to be a manual process. There’s no way for an automatic script to know what post to attach to well after initial upload.

    Thread Starter trejder


    Of course, you can always attach an unattached image after the fact, but it has to be a manual process.

    And the purpose of this whole tread was to ask, how can I do this?

    Is there a relatively easy way of correcting such unattached images? By manually attaching them back? If yes, then can you elaborate it? Thank you in advance.

    By “relatively easy” I mean — using standard WordPress administration panel or some plugins, if needed. If above requires manual database editing or changing WordPress source code then such solution is out of the question as for me.

    Moderator bcworkz


    I was hoping to solve the problem so it doesn’t happen and you don’t need to attach retroactively. Attaching a few images is simple and does not require plugins or special coding. Attaching hundreds of images becomes quite tedious.

    Attach from the media library list table. Pick the detail list view if you have the grid view. (icon in action bar near the top). There’s an “Uploaded to” column that lists the post each media item is attached to or if it’s unattached. Below that is an Attach/Detach link. The Attach link puts up a modal box which lets you pick or search for the right post to attach. After picking a post, click select.

    Thread Starter trejder


    Thanks for trying to help. To summarise this topic:

    a) never do two step media upload (via Media library), always attach image directly to post,

    b) the only way to cleanup unattached images is to simply delete them and upload them again, to corresponding posts, directly from post editing screen.

    For blogs and sites in blog network that has 20-30 unattached images this maybe considerable, if someone needs this. For hundreds of unattached images this seems to be pointless and worthless. Thank you.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘A lot of images reported as being unattached’ is closed to new replies.