DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Attach Unattached Media Files

Posted by on

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.

Attach Unattached Media Files - Step 1: Find unattached media files

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:

Attach Unattached Media Files - Step 2: Click the 'Attach' button

After clicking the “Attach” link, a popup window will appear, like so:

Attach Unattached Media Files - Step 3: Enter the post title and click search

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).

Attach Unattached Media Files - Step 4: Choose the correct post and click 'Select'

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.

1-minute summary

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 :)

14 Responses

  1. stvwlf May 1, 2012

    Two plugins in WP repository for Unattach

    current
    http://wordpress.org/extend/plugins/file-un-attach/

    through 3.1.4 – may still work
    http://wordpress.org/extend/plugins/unattach/

  2. DeMomentSomTres May 1, 2012

    Great post giving a global vision of attachment files in WordPress.
    I was trying to post the method to unattach a post but I found the comment by stvwlf with a plugin. So I didn’t post it.

  3. Sometimes I use the media library just to host images of files without them necessarily being attached to a page or post. Is there a problem that I may be causing by doing this? It seems that everyone always wants to have all files “attached” but is it necessary from a technical point of view?

    • Ah good point – you are correct of course, in that it’s totally fine to have unattached media files of any type. I’m looking at it more from an organizational perspective in this article. You’re all good.

      • Right on. I do wish the media library built-in were more robust… but I’m sure over time and some revisions it will get more attention.

  4. Another issue with attaching media is that the search box that pops up from the media library “attach” link doesn’t seem to work with custom post types. There is a radio button for each custom post type, but doing a search only seems to search regular posts.

  5. Matth May 10, 2012

    Attaching seems like an anal exercise with no tangible benefits.

    • I use the Media Library all the time, bulk uploading images and then attaching to their respective posts. Way easier than doing it individually post by post.

    • I can say that about a lot of things LOL.

      But yes, I agree… attaching for the sake of attaching… no point. But attaching with a purpose, like to use a custom gallery or do some fancy presentation of the different types of files, then I can understand its usefulness.

    • I’m guilty of attaching unattached media files just so they’ll be attached. No point in having useless/unused files laying around, just increases liability imo. It’s personal preference for the most part.

  6. Is there a faster way to attach nearly 14,000 images into hundreds of post ? because if done manually would take a long time…

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry