Get all WordPress users with a certain role

The code snippet in this article shows you how to get all WordPress users with a certain role, and the options available to further filter results.

Limiting returned users to specific arguments is easy with the WordPress get_users function. The following code snippets show you how.

To get all WordPress users with a specific role you'll want to pass in the 'role' argument to the get_users function like so:

get_users( 'role' => 'administrator')

WordPress has six default roles that are pre-defined: super admin, administrator, editor, author, contributor and subscriber. You can replace these in the role argument above, or if you have a custom role that you have registered you could also use that.

Here's a full list of arguments for the get_users function if you want to add some additional parameters to filter or order the data:

  • blog_id - the current blog id
  • role - limit to specific user type
  • include - specific user ids to be returned in result set
  • exclude - specific user ids to exclude from the result set
  • meta_key - restrict results based on a specific meta key
  • meta_value - the meta value associated with the meta_key search
  • meta_compare - the comparison to be made for the meta_key and meta_value (e.g. '=')
  • orderby - the return order of the result set
  • order - ASC or DESC (ascending or descending)
  • offset - skip first part of result set useful for paging
  • search - for searching against email, url, id or username
  • number - limit total results
  • fields - specific fields to be returned in the array, by default everything is returned

Sign Up
comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.