How can I hide media library images from general users?

To let the current users only view his/her uploaded attachments, add the following code to your themes actions:

add_filter( 'posts_where', 'devplus_wpquery_where' );
function devplus_wpquery_where( $where ){
    global $current_user;

    if( is_user_logged_in() ){
         // logged in user, but are we viewing the library?
         if( isset( $_POST['action'] ) && ( $_POST['action'] == 'query-attachments' ) ){
            // here you can add some extra logic if you'd want to.
            $where .= ' AND post_author=".$current_user->data->ID;
        }
    }

    return $where;
}

Hope this helps!

Leave a Comment