Question

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?

Was it helpful?

Solution

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);
    }
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top