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 ?
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 ?
Share Improve this question asked Feb 1, 2021 at 15:11 lucrecelucrece 1248 bronze badges 2 |1 Answer
Reset to default 1One 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' )
.
user1
having the rolessubscriber
andcustom_role
? – Sally CJ Commented Feb 1, 2021 at 16:03user1
issubscriber
anduser2
issubscriber + manager
, I need to get onlyuser1
– lucrece Commented Feb 1, 2021 at 16:14