2

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?

2
  • 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
1

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) {
        setup_postdata($post);
        the_attachment_link($post->ID);
    }
}
10
  • @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
1

If you need that in your UI to manage those:

/wp-admin/upload.php?detached=1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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