Limit Media Library to Given Folder

I have made a plugin that uses the Media Library to allow users to upload files to a specific directory – using the upload_dir filter.

I would like to know if there is a way (i.e. a filter) I can use to limit the media library to displaying only files contained within my custom folder?

Read More

If possible, I want the user to be able to choose only files that have been uploaded to the custom folder when interacting with the Media Library instantiated by my plugin.

Related posts

Leave a Reply

1 comment

  1. A solution that works for me is to add a clause to the WordPress query when the media library is being displayed.

    From browsing my WordPress database I noticed that the full path to wp_posts.post_type = 'attachment' is stored in the wp_posts.guid column.

    add_filter('posts_where', 'limitMediaLibraryItems_56456', 10, 2 );
    function limitMediaLibraryItems_56456($where, &$wp_query) {
        global $pagenow, $wpdb;
    
        // Do not modify $where for non-media library requests
        if ($pagenow !== 'media-upload.php') {
            return $where;
        }
    
        $where .= " AND {$wpdb->posts}.guid LIKE '%my-path-segment%'";
    
        return $where;
    }