Question

I need to load only the subscribers

$args = array('role' => 'Subscriber');
$subscribers = get_users($args);

but with this, it returns also the users that are subscriber AND other role (I need to delete my subscribers only, but keep subscribers with other role)

How do I get this users ?

Was it helpful?

Solution

One possible way is by using the role__not_in parameter like so which excludes all other roles except subscriber:

// Include only the following role(s):
$roles_in = array( 'subscriber' );

$roles_not_in = array_diff(
    array_keys( wp_roles()->get_names() ),
    $roles_in
);

$args = array(
    'role__not_in' => $roles_not_in,
);

$subscribers = get_users( $args );

But of course, if you know the exact roles that should be excluded, then just do 'role__not_in' => array( 'role', 'role2', 'etc' ).

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top