Hide WordPress Posts and Media uploaded by other users

It is pretty cool how wordpress manages user and restrict access via defined roles but one of the critical part of the website are the images. Once you give access to your editors they will have capability to view and delete images even if they are not uploaded by them which will ruin the pages and posts that are using it. On some points you don’t want the site users, freelancers or even general public to access the images. Well then you came to the right page :)

This tutorial also covered the ability to hide the posts that are created by other users so that the current users cannot make any changes.

First you need to be able to access the editor by navigating to Appearance > Editor and find the theme’s functions.php

That’s great you have the required access ;)

Add the code below which will hide the posts and images uploaded by other users except for administrator on /wp-admin/edit.php and /wp-admin/upload.php

Easy isn’t it? Now we need to be able to hide the images when the user click the ‘Add Media’ button above the wordpress editor. Which is pretty much easy too by adding this code :

Works like charm! Special thanks to @timocouckuyt for providing that code on this forum post.


About Jeffrey

My name is Jeffrey Carandang a.k.a 'phpbits' and I'm a web developer. I love creating unique, clean, usable code for the web and other digital sources and my utmost priority is my customer’s satisfaction.

Increase reader engagement by using WP Author Box Lite for Free. Undecided? Read the plugin review first.


  1. Juan Pablo says

    This post is excellent. Thanks a lot! Really :-)

    I have a question. There is a situation in which the user can see the image, when he use directly URL like this:


    Do you know if we can hook on that? Thanks for any tip.

  2. says

    Hello Jeffrey,

    This is a great post. Thank you.

    I had a similar concern but I only took into account a specific user type. So far I don’t see any problem. I’m new to WordPress and I’m not quite familiar with PHP. After some research and tweaking, I came up with the code below. Do you see any issues that it may cause?

    if ( current_user_can(‘contributor’) ) {
    add_filter(‘parse_query’, ‘limit_media_files_access’ );
    if( !current_user_can(‘upload_files’) )
    add_action(‘admin_init’, ‘allow_contributors_upload_files’);

    function allow_contributors_upload_files() {
    $contributor = get_role(‘contributor’);

    function limit_media_files_access( $wp_query ) {
    global $current_user;
    $wp_query->set( ‘author’, $current_user->id );

    • Jeffrey Carandang says


      If this is working our for you that’s fine :) The problem here is that you are removing the ability to upload images on user roles below contributor and other user roles can access all the images. You can merge this with my code above and it will be perfect for your needs!


  3. says

    Hi, it’s possible to disable this code for some custom post types only?

    For example, i need logged users to view other’s pages only, instead of articles.

    Thanks in advance!

  4. Sam Elliott says

    Great post, but I cannot get this to work within custom post types? Can you help me please, or advise where I should be looking?

  5. kaffeeguru says

    Thanks for that post. It helped me to get on the right track

    Another case where you need to place the filtering is ajax requests.

    In my current WP the media library is requested via admin-ajax.php

    The WP codex already gives an example how to use filtering here:


    add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );

    function show_current_user_attachments( $query = array() ) {
    $user_id = get_current_user_id();
    if( $user_id ) {
    $query['author'] = $user_id;
    return $query;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">