Skip to content

Instantly share code, notes, and snippets.

@fastmover
Created February 10, 2016 22:23
Show Gist options
  • Select an option

  • Save fastmover/d028b6984ea946f221d5 to your computer and use it in GitHub Desktop.

Select an option

Save fastmover/d028b6984ea946f221d5 to your computer and use it in GitHub Desktop.
WordPress: Allow the user to only see media they've uploaded.
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( !in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) ) )
return;
if( ! current_user_can ( 'administrator' ) && ! is_admin () )
$wp_query_obj->set('author', $current_user->ID );
return;
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment