Attaching any unattached media files that you may have floating around is a good way to keep things organized and running smooth. Normally, when you're working on a post in the Edit Post screen, you click the Upload/Insert button and use the nifty drag-n-drop media uploader to get 'er done. When you upload your media files in this manner, WordPress "knows" that you want to attach the file to that particular post. Super straightforward sure, but there are situations where WordPress doesn't know which post to use. In this DigWP post, we walk through the process of finding unattached media files and attaching them to their respective posts.
Why media files may be "unattached"
Basically, WordPress needs to know which post to attach the file to, so if that information isn't available, the file's status is set to "unattached". For example, if you have a bunch of images attached to a post, and then delete the post, the images are still visible in the Media Library and still exist in the
/wp-content/uploads/ directory, but they will be unattached to any particular post.
Another reason why you might have unattached media files is because they were uploaded directly through the Media Library instead of through a specific post. Some WordPress users like to upload lots of images up-front, before creating any posts. Are there other ways that media files become unattached? Possibly, but ultimately it doesn't matter because it's relatively straightforward to locate all unattached files and attach them to specific posts. Well, it is once you know how to do it..
How to find your unattached media files
Eliminating and/or organizing rogue files of any type is good practice for running a tight ship, whether it be media files, theme files, or other project files laying around on the server. When it comes to cleaning up your media files, WordPress provides two easy ways of locating everything that's not attached to a specific post.
As seen in the previous image, the first and easiest way to find and attach your files is to click the Unattached link (1) at the top of the Media Library page. That will list all of your unattached files along with a quick link to Attach (2) the file to a specific post. This is also the second way to find unattached images, as the "Attach" link is easy to spot among all of your attached media files.
How to attach your unattached media files
Once you've found some unattached files, you're ready to attach them to their rightful posts. There are several ways to do this, depending on where you're at in the WordPress Admin.
- If you're on the Media Library page, then you'll see the Attach link next to the unattached file (see previous image).
- If you're already viewing the list of "Unattached" images, you can hover over a specific file to reveal its Attach link.
- If you're working on a post, click the Upload/Insert button, go to the Media Library tab, search for the image, and click the Insert into Post button.
Here is a screenshot showing the "Attach" link next the file in the list of "Unattached" files:
After clicking the "Attach" link, a popup window will appear, like so:
At this point we see some room for improvement, in that you sort of have to "know" in advance the title or contents of the post to which the file should be attached. If you're not sure and just want to sort of browse through existing posts to find the best one, try searching with some relevant/related search terms. If you really have no idea which post to use, try searching for a super-common word like "the" or "a" and you'll get the first 30 or so posts.
Unfortunately at this time there is no way to navigate through more than the first set of results, but it may be enough to get you there. It would be good to have this option in the future (hint hint).
Once you've found the post that you would like to use, select it and click the Select button, as shown in the previous image. That's all it takes, after clicking "Select" WordPress does its thing and the file is attached to the selected post.
How to un-attach media files
Wrapping up, just wanted to point out another area where WordPress could be improved. As far as I know, there is no way to "un-attach" media files from their respective posts from within the Admin area. Unless somebody knows a way of doing so, the only way seems to be complete deletion of the media file in question. When working with a lot of files, each with their own meta information, this can be a rather unpleasant experience, but I digress..
Yes you can always attach the file to another post, but that doesn't change the post to which it was first attached. To change that information, it's either a trip to the database or delete the file and start over.
To attach an unattached media file, find an Attach link, select the desired post from the list, and click the Select button. Alternately, if you're working on a post, click the Upload/Insert button, visit the Media Library tab, locate and select the desired post, and click the Insert into Post button.
Taking the time to clean-up and organize your files is a great way to improve the efficiency and maintainability of your website.
That's all for now, Happy attaching :)