Show Users Only Their Own Media
Authors and contributors only see files they uploaded in the Media Library and media picker. Editors and administrators still see everything.
Opens WPIDE on
Code
add_filter( 'ajax_query_attachments_args', function ( $query ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
$query['author'] = get_current_user_id();
}
return $query;
} );
add_action( 'pre_get_posts', function ( $query ) {
global $pagenow;
if ( is_admin() && 'upload.php' === $pagenow && $query->is_main_query() && ! current_user_can( 'edit_others_posts' ) ) {
$query->set( 'author', get_current_user_id() );
}
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.