Ready to get started?Download WordPress


get users using get_users() by excluding a role? (4 posts)

  1. Peter vanDoorn
    Posted 1 year ago #

    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?



  2. Rebecca Schaefer
    Posted 1 year ago #

    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.

  3. Peter vanDoorn
    Posted 1 year ago #

    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


  4. bcworkz
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic