最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Load user by specific role

programmeradmin0浏览0评论

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
  • You mean, you have users who have two or more roles like user1 having the roles subscriber and custom_role? – Sally CJ Commented Feb 1, 2021 at 16:03
  • 1 Yes, user1 is subscriber and user2 is subscriber + manager, I need to get only user1 – lucrece Commented Feb 1, 2021 at 16:14
Add a comment  | 

1 Answer 1

Reset to default 1

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' ).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论