WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I have this code in my functions.php and its working well when the editor is viewing the Media library: http://example.com/wp-admin/upload.php


function users_own_attachments( $wp_query_obj ) {

global $current_user, $pagenow;

if( !is_a( $current_user, 'WP_User') )

if ( 'upload.php' != $pagenow ) 

if( !current_user_can('delete_users') )
    $wp_query_obj->set('author', $current_user->ID );


The above code will prevent other editors from viewing the images uploaded by other editors in the media library. Only the administrator can see all images uploaded. The editor can only see the images uploaded by themselves (not others).

Now here is the issue, when creating or editing a post, Wordpress has an "Add Media" button that will allow users to upload images to their gallery. But the code above does not work when the user will upload via "Add Media" popup. They can still see the images uploaded by other editors.

How is it possible to modify the above code so that it will still work when users are viewing images via the "Add Media" popup and not the media library? Thank you so much for your help.

share|improve this question
I found the following solution, take a look: wordpress.stackexchange.com/questions/1482/… I used the code provided in the second best answer. – riCo.ger Apr 10 '13 at 8:48
up vote 0 down vote accepted

If I correctly understand your question, you may try this

function wpse78084_show_media_by_current_author_only($query) {
  global $pagenow, $user_ID;

  if( 'upload.php' == $pagenow && !current_user_can('manage_options') )
    $query->set('author', $user_ID);

return $query;

if ( is_admin() )
  add_filter('pre_get_posts', 'wpse78084_show_media_by_current_author_only');
share|improve this answer

The feasible solution would be to run a conditional tag to determine if the user is viewing the add media popup, see it here: Conditional tag to determine if the user is viewing the Add Media (after clicking Add media button)

And then add it in additional to this statement:

if (( 'upload.php' != $pagenow ) || (is_viewing_media))
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.