Support » Plugins » Hacks » get users using get_users() by excluding a role?

  • I’m successfully building a list of users using get_users(), and I can see in the Codex that it’s possible to target the list using the ‘role’ argument, but what I can’t seem to fathom is if it’s possible to target the list to exclude a specific role.

    To clarify – get_users() will pull in all users, regardless of role, and get_users(‘role=subscriber’) will pull in just the subscribers, but if I want all users except for subscriber, I can’t see how to do it.

    Any ideas?



Viewing 3 replies - 1 through 3 (of 3 total)
  • I’ve never tried this. Have you tried adding a ‘-‘ before the role? Sometimes in other queries that excludes that entry, but this would be a fingers crossed moments.

    Your best bet, probably, would be to generate a custom query and use wpdb to fetch the results.

    Hmm… thanks for the suggestion, but it didn’t work. In other queries, adding ‘-‘ usually only works when IDs are involved.

    Dealing directly with the database via wpdb sounds just a little too scary for me, so I think I’ll try and find another solution.

    I’m off to research how I can read the user’s role, since it isn’t returned with the other get_users() info.

    Thanks again


    Moderator bcworkz


    Something else not tried but maybe worth trying is array('role'=>array('role1', 'role2', 'etc_roles')) assuming you don’t have dozens of roles to list.

    Getting roles is a bit convoluted IIRC. Cannot recall how it’s done unfortunately. If all roles have a capability that Subscriber does not, your task should be simplified. Limit list display by user_can().

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get users using get_users() by excluding a role?’ is closed to new replies.