Last active
December 15, 2020 12:42
-
-
Save messaoudi-mounir/8285459 to your computer and use it in GitHub Desktop.
Buddypress Exclude members by roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find this breaks pagination - did you have that issue? I've tried using the functions in the template directly and in functions.php Thanks for the tip!