# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: media, privacy, roles
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/restrict-media-to-own-uploads/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Show Users Only Their Own Media" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
