Media PHP Intermediate

Let Authors See Only Their Own Media

Show authors and contributors only the files they uploaded, in both the Media Library and the "Add media" picker.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 19 lines
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() );
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • This hides files in the admin only. Every uploaded file still has a public URL.
  • Authors can still use images they find by URL elsewhere on your site.

How to test it

Log in as an author and open Media → Library. You should only see files you uploaded.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only