[wp-hackers] Get All Users with Specific capability

Ryan Bilesky rbilesky at gmail.com
Tue Feb 15 23:59:39 UTC 2011


Ok so basically there isn't a better way.  Now I can make this easier by
caching the resulting array in say a site option, then most the time all I
have to do is pull a site option.  Then I just need to find the necessary
hooks pto update the data when it has or may have changed.  So I need to
find hooks for when role/capabilities are changed and a hook when a user
changes roles.  Does anyone know of any such hooks, am I missing any otherd
id need?

On Feb 14, 2011 8:16 PM, "Dion Hulse (dd32)" <wordpress at dd32.id.au> wrote:
> Why not get the list of roles which the cap it applied to then?
>
> As you can see, other than some dodgy LIKE on wp_capabilities (Which can
> contain roles AND the user might have a cap Singually outside of the
> role..) or looping over all users, there isnt a way at present.
> See: #10201: enhancement: Switch roles to use single role, and no
> user-specific caps - http://core.trac.wordpress.org/ticket/10201
>
> On Tue, 15 Feb 2011 12:19:46 +1100, Ryan Bilesky <rbilesky at gmail.com>
> wrote:
>
>> Im not basing it off of role though, so that won't work if the user
>> customizes wp to give other roles the capability.
>>
>> On Mon, Feb 14, 2011 at 5:15 PM, Alex Hempton-Smith
>> <hempsworth at gmail.com>wrote:
>>
>>> 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
>>> >
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>
>
> --
> Dion Hulse / dd32
> Contact:
> e: contact at dd32.id.au
> Web: http://dd32.id.au/
> _______________________________________________
> 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