Show authors and contributors only the files they uploaded, in both the Media Library and the "Add media" picker.
function nahnu_limit_media_to_own(): bool {
return ! current_user_can( 'edit_others_posts' );
}
// Grid view and the media picker in the editor.
add_filter( 'ajax_query_attachments_args', function ( array $query ): array {
if ( nahnu_limit_media_to_own() ) {
$query['author'] = get_current_user_id();
}
return $query;
} );
// List view at Media > Library.
add_action( 'pre_get_posts', function ( WP_Query $query ): void {
global $pagenow;
if ( is_admin() && $query->is_main_query() && 'upload.php' === $pagenow && nahnu_limit_media_to_own() ) {
$query->set( 'author', get_current_user_id() );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
On multi-author sites, everyone can browse every file in the Media Library, including other people’s drafts and private documents. This snippet shows authors only their own uploads.
The Media Library’s grid view and the editor’s media picker load files over AJAX using ajax_query_attachments_args. The list view uses the normal admin query. The snippet adds an author limit to both for anyone who can’t edit other people’s posts.
Editors and administrators still see everything.
Log in as an author and open Media → Library. You should only see files you uploaded.