Using WordPress ‘get_users_of_blog()’ PHP function

The get_users_of_blog() WordPress PHP function retrieves a list of users for a specific site.

Usage

get_users_of_blog( $id );

Example:

Input:

get_users_of_blog( 2 );

Output:

An array of users belonging to site with ID 2.

Parameters

  • $id (int) – Optional. The site ID for which to retrieve users. Default: ” (empty string).

More information

See WordPress Developer Resources: get_users_of_blog()

Examples

Get all users of the current site

This code retrieves all users of the current site.

$current_site_id = get_current_blog_id();
$users = get_users_of_blog( $current_site_id );

Display user names for a specific site

This code retrieves and displays user names for a site with ID 3.

$site_id = 3;
$users = get_users_of_blog( $site_id );
foreach ( $users as $user ) {
    echo $user->user_login . '<br>';
}

Count users of a specific site

This code counts the number of users for a site with ID 4.

$site_id = 4;
$users = get_users_of_blog( $site_id );
$user_count = count( $users );

Get users with a specific role from a site

This code retrieves all administrator users from a site with ID 5.

$site_id = 5;
$users = get_users_of_blog( $site_id );
$admins = array_filter( $users, function( $user ) {
    return in_array( 'administrator', $user->roles );
});

Get email addresses of users from a specific site

This code retrieves the email addresses of users from a site with ID 6.

$site_id = 6;
$users = get_users_of_blog( $site_id );
$user_emails = array_map( function( $user ) {
    return $user->user_email;
}, $users );