Retrieving User ID list for users with specific ccap

I am working on a php program and need to retrieve a list of users that have a specific custom capability. I have done quite a bit of searching through the codex and have only been able to find functions that require me to know the user ID already or default to the current user if not passed. I was able to find a shortcode that does part of what I want and put it in my php like this: $ccap_results = do_shortcode( “[s2Member-List ccaps=’$myccap’ /]” ); However, when I dump the results I get the users avatars with their names underneath and it is the correct list of users - but how do I pull out the list of user IDs from this? I don’t want to display this on a page - I just need the IDs in order to update other information for the list of users.

Thank you so much for your help!

Try this:

$desired_ccap = 'the_ccap';
$users = array();
$uccaps = $wpdb->get_results("select user_id, meta_value from $wpdb->usermeta where meta_key='".$wpdb->prefix."capabilities' && meta_value like '%".$desired_ccap."%'", ARRAY_N);
foreach($uccaps as $k => $ccaps) {
	$cap = array_keys(unserialize($ccaps[1]));
	foreach($cap as $i => $capname) {
		if(preg_match('/^access_s2member_ccap_(.*)$/', $capname, $m) and $m[1] == $desired_ccap) $users[] = $ccaps[0];
	}
}

Should give you users IDs in an array.

Wow! Thank you so much for this! It works like a dream and solves my issue perfectly. It would have taken me quite a bit of time to come up with this solution so I really appreciate you actually writing the code for me. I have learned quite a bit just from studying what you have done.

My pleasure! :slight_smile: Actually I just modify some code from a plugin of mine.