wordpress snippets last updated   : April 14, 2014
we now have 630 snippets

Restricting users to view only media library items they upload

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will restrict users to view only media library items they upload. They will still see the total number of files that are uploaded but will not be able to view them even if they type in the attachment_id.

snippet :  PHPcopy
function my_files_only( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
        if ( !current_user_can( 'level_5' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
add_filter('parse_query', 'my_files_only' );
source →
  • Anonymous

    This snippets is so helpful for my multi bloggers site. You should make it to WP plug-ins! thanks =)

    • http://wpsnipp.com Kevin Chard

      Cool glad you like the snippet, I’m not sure it is worth adding as a plugin although it could be setup as one.

      • Anonymous

        You should, I’m having hard time in the plug-ins pool finding this and end-up gave up and looking for external resources, that causes me to reached here. I love your blog, straight cut per purpose snippet. Learning a lot from here!
        You’d shined my day, Thanks Chard! =)

        • http://wpsnipp.com Kevin Chard

          No problem, Glad you like the website – visit regularly we post daily!

  • Anonymous

    Found an issue, media library still showing full content at post insert media popup window. (same as other pages template too)

    however, I added get global $post, and add another condition for posts.


    but it didn’t work..

    • lordarkad

      I fixed this by adding a second clause
      “|| strpos( $_SERVER[ 'REQUEST_URI' ], ‘/wp-admin/media-upload.php’ ) !== false”

      • designlabCPH

        How on earth do you get this to work in WP 3.5?

        I’ve tried everything (I belive, although I’m not a PHP coder) including adding your solution above – and I just can’t get it to work.

        I need that only Admins can see all media in the library pop-up, and everyone else (including editors) can only see their respective images…. Would you mind spelling it out for me? :)

        • http://www.facebook.com/jlamarferren J.Lamar Ferren

          Use Role Scoper if you haven’t gotten it figured out yet man. It accomplishes this very issue. It’s by default when you install the plugin. Both the Media Library and posts. FYI

          • Ari

            I have spent hours today just to fix that users upload issue, and found that’s the only code that work. Thanks!

        • Patrick

          You have to change a few parameters for this to work with the new media manager.

          You can use this function (notice that I created a “manage_media_library” capability. Only if the user has this, will they see other files than their own):

          function ml_restrict_media_library( $wp_query_obj ) {

          global $current_user, $pagenow;

          if( !is_a( $current_user, ‘WP_User’) )


          if( ‘admin-ajax.php’ != $pagenow || $_REQUEST['action'] != ‘query-attachments’ )


          if( !current_user_can(‘manage_media_library’) )

          $wp_query_obj->set(‘author’, $current_user->ID );



          • Lorenzo

            I was looking exactly for this: ‘admin-ajax.php’ != $pagenow || $_REQUEST['action'] != ‘query-attachments’
            thanks so much!

          • Wizzard

            Where did you put this?

          • wizzard

            as a new plugin! good, this works for me!

          • jimii

            great code solution Patrick – fixed my issue pronto – I wish I had found this back in Dec. : ) thank you!

          • Mickael

            I try your code in plugin, directly in function.php but it doesn’t work. I’ve WordPress 3.5.1 ans i want to use it in front end with the add media button of the wp_editor function.
            I’ve tried in wp-admin but it doesn’t work to.

            I’ve you any idea why it’s not working ???

          • Xand0r

            Used this, much kudos to you!

  • http://www.facebook.com/people/Sheila-Tan/100000231107373 Sheila Tan

    thanks for the post. just what i was looking for. 
    i made a small change >> also add the file media-upload.php to this line of code. this will also filter the images /mediafiles above the wysiwyg editor.

    • Ldamian

      Hi Sheila,

      Where/What did you post code-wise in the media-upload.php file? I’m getting an error when I paste the code in. Thanks in advance!

  • sonic

    Thanks Kevin,  it is very useful, but how to fix  ’total number of files’ ?

    • http://wpsnipp.com Kevin Chard

      Glad to hear you like the snippet! Well the total is still correct since the files do exist, although I do see your point. Ill take a look into this in the near future see if I can post a snippet.

  • http://www.ariona.net Rian Ariona

    I have one question kevin, how to restrict contributor from uploading media?

    • http://wpsnipp.com Kevin Chard

      Hi Rian,
      On line 3 you will see “level_5″ on this wordpress page is has details on what the user levels are you can just change that level. http://codex.wordpress.org/User_Levels#User_Level_5_2

      • http://www.ariona.net Rian Ariona

        i didn’t notice that default role for contributor is can’t upload any media. i just try it last night.. haha.

        Thanks you very much Kevin! :)

        • http://wpsnipp.com Kevin Chard

          No problem anytime,

  • Britt

     Using WP 3.3.1 and this is not working. Is their an update on the code? Thanks.

    • Patrick

      Look at my reply above. I posted a function that will work with the new media manager (WP 3.5+)

  • Guest

    is there a way to program this line of code to only apply this to specific membership levels? (i.e. contributors are only able to see their media uploads, but admins can see everything)

  • http://www.facebook.com/jpgphotography Jashua Peter Grafstein

    is there a way to set up this line of code to restrict the media library for contributors only. That way contributors can only see media they upload and admins can see everything.

  • http://www.facebook.com/jlamarferren J.Lamar Ferren

    I used the Role Scoper plugin. I’ve been looking to do figure this out all day. Role Scoper will only allowed the logged in user to see their own content and own posts by default. You can change this if you want to as well. Private Media Library for Authors ;-)

  • Lorenzo

    Does anyone know how to restrict user also when they upload file from the insert or edit post? In fact when I press the Add Media button, the popup shows me the Media Library tab, with all the media uploaded by all users. I’m using the WP 3.5.1.

    • Patrick

      Look at my reply to designlabCPH above. Insert this function in your functions file, and the media library will be restricted to users’ own files only.

  • Christopher Grasso

    Thank you a ton for this…. Role scoper was messing with another plug in and thats the only feature i wanted was the images to be limited… Thanks for updating it to work with the new media library, it works flawlessly.

  • ?afak SARAÇO?LU

    Çok te?ekkür ederim – Thank you

  • Din Bogdan

    I put that code… but have wrong counter for media post…

    Curent user not have media post … whi have that All (15) | Images (15) | Unattached (2)

    i want show count for curent user.. not for all….

    • John Xiao

      I just hide all count numbers by css: subsubsub .count {display: none; }

  • WP

    hi, thanks for the code. very helpful!

    however, other users are still able to view the attachments if they have a direct link. is there a way to prevent this? as in only the owner of the media (and admin) are able to see the attachments. other users or public cant seem them.


  • Mooce

    It worked! (WP 3.8.1) Thank you!

  • Pingback: My new best friend: ajax_query_attachments_args | Alicia Ramirez

jqsnipp.com | we just launched jquery snippets website