[wp-hackers] Get All Users with Specific capability

Alex Hempton-Smith hempsworth at gmail.com
Tue Feb 15 01:15:44 UTC 2011


You could get the user ID's from a DB query like this:

$wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key =
'wp_capabilities' AND meta_value LIKE '%administrator%' ");

That's the general idea anyway.

Kind regards,
Alex Hempton-Smith

www.alexhemptonsmith.com
www.twitter.com/hempsworth



On Tue, Feb 15, 2011 at 1:10 AM, Ryan Bilesky <rbilesky at gmail.com> wrote:

> I need to get all users who have a specific capability, I have a function
> with this code:
>
> global $wpdb;
> $members = array();
>
> // Get all user ID's
> $members_search = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY
> ID");
>
> foreach ($members_search as $userid) {
>     // Check each user ID, check for cap
>     if (user_can($userid,  'manage_options')) {
>         $members[] = $userid;
>     }
> }
>
> return $members;
>
> I was just wondering if there is a better way perhaps?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list