Using WordPress ‘get_users()’ PHP function

The get_users() WordPress PHP function retrieves a list of users matching specified criteria.

Usage

$users = get_users( $args );

Example:

$args = array(
    'role' => 'subscriber'
);
$users = get_users( $args );

Parameters

  • $args (array|string): Array or string of query parameters for filtering the users.

More information

See WordPress Developer Resources: get_users()

Examples

Get all users with the ‘editor’ role

$args = array(
    'role' => 'editor'
);
$editors = get_users( $args );

Get users with a specific meta key and value

$args = array(
    'meta_key' => 'user_location',
    'meta_value' => 'New York'
);
$new_york_users = get_users( $args );

Get users with ‘subscriber’ role, excluding specific user IDs

$args = array(
    'role' => 'subscriber',
    'exclude' => array(1, 2, 3)
);
$filtered_subscribers = get_users( $args );

Get users matching a search keyword

$args = array(
    'search' => '*john*'
);
$john_users = get_users( $args );

Get users sorted by display name in descending order

$args = array(
    'orderby' => 'display_name',
    'order' => 'DESC'
);
$users_sorted = get_users( $args );