Home / functions.php / List all unattached files in media library
  • How to List all unattached files in media library

    list-all-unattached-files-in-media-library

    Adding the first snippet to the functions.php of your wordpress theme then add the second snippet into your wordpress template. Once added this snippet will display thumbnails of unattached image files and links to other file types.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function get_attachment_files(){
    $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);
        }
     }
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <? get_attachment_files(); ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    attachment_link, get_post, get_posts, setup_postdata,