Skip links

How to restrict own media per wordPress user without plugin?

You see by default image uploaded to WordPress media library are to be seen everyone logged in and has access to media library. But this will be an issue if you have multiple user, authors, vendors which has their own part.

Today I choose to help you solve that issue without installing any plugin.
You’ll need to add the following code to your WordPress functions.php file or a site-specific plugin.
Is better to add this to a child theme so you won lost this code during updates.

NB: If you don’t have the option to edit your theme or plugins, make sure you disable the security plugin if you are using one e’g siteground security.

// Limit media library access
add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );
function wpb_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
        $query['author'] = $user_id;
    return $query;

This code uses current_user_can function to check if the user has the capability to activate plugins or edit other user’s posts. If they don’t, then it changes the query used to display media files and limit it to user’s ID.

Or use the following

<?php // Limit access to media library (users can only see/select own media) //
   add_filter( 'ajax_query_attachments_args', 'wpsnippet_show_current_user_attachments' );
   function wpsnippet_show_current_user_attachments( $query ) {
      $user_id = get_current_user_id();
      if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts')) {
         $query['author'] = $user_id;
      return $query;

Leave a comment

This website uses cookies to improve your web experience.
Awesome website in making

We design  powerful websites never done before !! subscribe to our mail list to receive super contents help you start and grow your online business. 

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.