How can I filter members according to WP user role

I'm banging my head against the wall with this one. I'm sure it's super easy, but I just can't find the right snippet to get it done.

So, I'm using Membership and Buddypress. Users sign up for a subscriptoin, which assigns them either the WP user role of "Family" or "Service".

Now, in the Buddypress membership directory page, I don't want "Family" WP user role users to be able to see other "Family" user role members. This would be pointless, because they only want to meet "Service" members.

Is there a way to filter the view of the Buddypress directory page so that members with a WP role of "Family" only see results with members that have the "Service" WP user role?

I'd love any feedback or help on this.

  • Kasia Swiderska
    • Support nomad

    Hi Ronel,

    I don't think it's super easy to achieve. It would require code snippet that will
    1) Check what is role of person that is viewing the membership directory
    2) Depending of role in first step, exclude users of roles from list.

    I found this snippet for excluding certain users from list
    http://buddydev.com/buddypress/exclude-users-from-members-directory-on-a-buddypress-based-social-network/
    so it should be combined with checking of user role https://buddypress.org/support/topic/display-users-role-on-the-members-loop-php-file/

    If writing this code snippet is not an option for you, I could provide you that code, but it will take me some time to write it and test it. How that sounds?

    Kind regards,
    Kasia

  • Ash
    • WordPress Hacker

    Hello @Ronel

    I hope you are well today.

    Please try the following code:

    add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 );
    function bpdev_exclude_users( $qs=false, $object=false ){
    
    	$user = wp_get_current_user();
    	if ( in_array( 'family', (array) $user->roles ) ) {
    		$role = 'family';
    	}elseif ( in_array( 'service', (array) $user->roles ) ) {
    		$role = 'service';
    	}else{
    		$role = false;
    	}
    
    	if( ! $role ) return $qs;
    
    	$includes = array();
    	$users = get_users( array( 'role' => $role ) );
    	foreach( $users as $user ){
    		array_push( $includes, $user->ID );
    	}
    
    	if( $object != 'members' )
    		return $qs;
    
    	$args = wp_parse_args( $qs );
    
    	if( ! empty( $args['user_id'] ) )
    		return $qs;
    
    	if( ! empty( $args['include'] ) )
    		$args['include'] = $args['exclude'] . ',' . implode( ',', $includes );
    	else
    		$args['include'] = implode( ',', $includes );
    
    	$qs = build_query( $args );
    
    	return $qs;
    
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Please note that, this code only affect family and service role users. Other users will be able to see all members.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ronel
    • Design Lord, Child of Thor

    Hi Ash,

    Thank you so much!

    I've added the snippet to functions.php, and there's definitely filtering going on, but not as expected. I attach a screenshot.

    I logged in as a "Family" member, with a user sporting the "Family" WP user role. When i visited the buddypress /members/ page, I was only shown one member. Myself.

  • Ash
    • WordPress Hacker

    Hello @Ronel

    I hope you are well today.

    How the roles were created? Using user role editor plugin? Or any third party plugin? Would you please make sure that every user has only one role and Friends and Services are not additional role beside Subscriber?

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Ohh also I have modified a little the above code, please give it a try too:

    add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 );
    function bpdev_exclude_users( $qs=false, $object=false ){
    
    	$user = wp_get_current_user();
    	if ( in_array( 'family', (array) $user->roles ) ) {
    		$role = 'service';
    	}elseif ( in_array( 'service', (array) $user->roles ) ) {
    		$role = 'family';
    	}else{
    		$role = false;
    	}
    
    	if( ! $role ) return $qs;
    
    	$includes = array();
    	$users = get_users( array( 'role' => $role ) );
    	foreach( $users as $user ){
    		array_push( $includes, $user->ID );
    	}
    
    	if( $object != 'members' )
    		return $qs;
    
    	$args = wp_parse_args( $qs );
    
    	if( ! empty( $args['user_id'] ) )
    		return $qs;
    
    	if( ! empty( $args['include'] ) )
    		$args['include'] = $args['include'] . ',' . implode( ',', $includes );
    	else
    		$args['include'] = implode( ',', $includes );
    
    	$qs = build_query( $args );
    
    	return $qs;
    
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ronel
    • Design Lord, Child of Thor

    Hi Ashok

    How the roles were created?

    The version of Membership plugin i'm using still supports new members being allocated a member role along with a subscription. So the Membership plugin itself creates the roles once the user confirms/pays his subscription.

    Would you please make sure that every user has only one role and Friends and Services are not additional role beside Subscriber?

    How would I check this? Under the Users tab on WP admin, the role shows only "Family" or "Service".

    I will give the modified code a try and let you know in an hour or two.

  • Ash
    • WordPress Hacker

    Hello @Ronel

    I will give the modified code a try and let you know in an hour or two.

    Sure, let me know.

    If it doesn't work, would you please send me login details and FTP details?

    To send me details, please use our contact form: https://premium.wpmudev.org/contact/

    Select: I have a different question
    Subject: Attn-Ash (this ensures that it will be assigned to me)
    Details:
    1. Send all requested details
    2. Send a link of this thread so that I can track
    3. Send any other relevant link

    I will be happy to take a look :slight_smile:

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.