Skip to content

Instantly share code, notes, and snippets.

@prasadnevase
Last active October 9, 2015 04:47
Show Gist options
  • Save prasadnevase/697348add446fad1f312 to your computer and use it in GitHub Desktop.
Save prasadnevase/697348add446fad1f312 to your computer and use it in GitHub Desktop.
WordPress Code to show media of logged in user in the media library. He won't be able to access media from other users.
add_action( 'pre_get_posts', 'users_own_attachments');
function users_own_attachments( $wp_query_obj )
{
global $current_user, $pagenow;
if ( $pagenow == 'upload.php' || ( $pagenow == 'admin-ajax.php' && !empty( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'query-attachments' ) ) {
$wp_query_obj->set( 'author', $current_user->ID );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment