Limit Media Library to Given Folder

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

Leave a Comment