How could users in a "customer" role be retrieved from a MembershipUserCollection?
To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there. $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("subscriber", $user_roles)){} Results in a role check for subscriber.
Required Editions and User PermissionsFrom Setup, in the Quick Find box, enter Roles , then select Roles. If the “Understanding Roles” page is displayed, click Set Up Roles. Find the role under which you want to add the new role. Click Add Role.
Roles.GetUsersInRole
returns a string[]
of user names in a role. If you really want the MembershipUser
objects, you can use:
var list = Roles.GetUsersInRole("roleName").Select(Membership.GetUser).ToList()
Of course, this is performance intensive as it hits the database once for every user.
If you are willing to give up provider-independence, you can query the underlying database directly and perform a join on the database server to get all users in a specific role.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With