WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my functions.php, I have a need to list all images in the uploads folder which are not currently attached to a post in the WP database.

It appears that every time an image is uploaded to the WP uploads folder (via FTP or via Media Manager), a records gets inserted in the WP database, right?

How can I obtain a list of all images that are not currently attached to any post?

share|improve this question
Just curious, is this somehow related to your other question? If so, I think there is some plugin that inserts all non-associated files in wp-uploads to the library as un-attached / detached entries into the database. – hakre Nov 11 '10 at 0:14
Hakre, are you referring to.. wordpress.org/extend/plugins/add-from-server – t31os Nov 11 '10 at 14:19
up vote 1 down vote accepted

This should work:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => 0
$attachments = get_posts($args);

if ($attachments) {
    foreach ($attachments as $post) {
share|improve this answer
@sorich: Thanks very much. This definitely gets me much closer. Its returning a bit more data than I need though. For example, it appears that the image itself is contained in ["guid"]=> string(59) "localhost/mysite/wp-content/uploads/theImage.png"; so that's the part I'm looking for. Any way to call get_posts and just return the "guid" value? – Scott B Nov 13 '10 at 23:45
I would not recommend using the guid as it was not meant to be used as link. The guid format may change in the future (core.trac.wordpress.org/ticket/6492). You should use the_attachment_link($post->ID, false), e.g., to get the link to the image. – sorich87 Nov 14 '10 at 10:51
In WP 3.1, you will be able to get only one field with get_posts: core.trac.wordpress.org/ticket/14777. In the meanwhile, you can use the filter posts_fields. – sorich87 Nov 14 '10 at 10:54
Not true, the patch does not specify that only one field can be returned, it's extending get_posts to allow selection of given fields, rather than simply returning all, this brings get_posts inline with other functions that support the same (one example would be get_terms). – t31os Nov 14 '10 at 12:23
@t31os: based on your explanation, I don't see how what I said is not true. I did not say that only one field will always be returned. What is the difference between "only one field can be returned" and "allow selection of given fields" (if I can select only one field e.g.)? Am I missing something? – sorich87 Nov 14 '10 at 15:29

If you need that in your UI to manage those:


Add the address to your blog in front.

Or more descriptive:

Log into your admin then use the menu: Media -> Library. Select the Unattached link above the lists' filter drop-down.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.