public
Last active

Buddypress Exclude members by roles

  • Download Gist
exclude-members-by-roles.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
function get_user_ids_by_role($role){
$users=array();
$founded_users = get_users( array( 'role' => $role ) );
if(!empty($founded_users)){
foreach((array)$founded_users as $user)
$users[]=$user->ID;
}
 
return $users;
}
 
function exclude_by_role( $exclude_roles, $implode = true ) {
$memberArray = array();
foreach ( $exclude_roles as $exclude_role ) {
$memberArray = array_merge($memberArray, get_user_ids_by_role($exclude_role) );
}
 
if( !$implode )
return $memberArray;
 
$theExcludeString = implode( ",", $memberArray );
return $theExcludeString;
}
 
 
add_action('bp_ajax_querystring','bp_exclude_byroles',20, 2);
function bp_exclude_byroles($qs=false,$object=false){
 
if($object!='members') //hide for members only
return $qs;
 
$args = wp_parse_args($qs);
//check if we are searching for friends list etc?, do not exclude in this case
if(!empty($args['user_id']))
return $qs;
 
$excluded_roles = array( 'administrator', 'subscriber' ); // you can add roles here
$exclude = exclude_by_role( $excluded_roles );
 
if(!empty($args['exclude']))
$args['exclude']=$args['exclude'].','.$exclude;
else
$args['exclude'] = $exclude;
$qs = build_query($args);
return $qs;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.