I want to list images for only user uploaded image. Here is the scenario:
- Using the image uploader on front end using iframe.
- I have added
upload_files
cap to subscriber level users and want them to see only images they uploaded. -
I’ve found this Q Restricting users to view only media library items they have uploaded? but the accepted answer not working for me.
// Code originally by @t31os add_action('pre_get_posts','users_own_attachments'); function users_own_attachments( $wp_query_obj ) { global $current_user, $pagenow; if( !is_a( $current_user, 'WP_User') ) return; if( 'upload.php' != $pagenow ) return; if( !current_user_can('delete_pages') ) $wp_query_obj->set('author', $current_user->id ); return; }
-
The frontend page i am using the image uploader is named “Entry”.
- If i remove this part of the code
if( 'upload.php' != $pagenow ) return;
the code works. I i think i have to improve that part of the conditional code but could not figure it out. The conditional is important because i don’t want the code to apply on other pages where it not required.
So, i need help to improve the conditional part as i mentioned. Thanks!
This works for me in order to list the items uploaded by a user on the media library.
It will allow all users with the
manage_options
capability (usually admin) to see all images, the other users will see only their own images. Note that a few improvements can be made like$pagenow
andcurrent_user_can
. Not a beauty but it does the jobThe media upload form that i was pulling up via thickbox was
media-upload.php
. So, i had to change the conditional toif( 'upload.php' != $pagenow && 'media-upload.php' != $pagenow)
. I forgot to look into the obvious place 🙂