• Resolved Dandy

    (@dandyjefferson)


    Hi, there

    Thanks for creating such fantastic plugin and provide supports all the time.

    I just have a quick question here that is it possible that after searching for values, the member searching range will be limited within User’s own Group? I’m using the BuddyPress and the site is used in the school environment. In most situation, teachers don’t want their students to search friends out of their schools or classes.

    So, I’m wondering is there any ways to limit the searching range from site-wide to own Group only? For example, if User A wants to search for friends in School A, he can only see result of his own Group members.

    Thank you very much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • mattrea

    (@mattrea)

    Hi, sorry I don’t know the answer but I’m following this as I have the same issue. Thanks, Matt.

    Thread Starter Dandy

    (@dandyjefferson)

    Hi, @mattrea

    I found this solution from someplace else (thanks for Buddydev.com) and it worked perfectly for me. So I shared it here and hope it will probably solve your issue as well.

    /**
     * Limit BP profile search plugin to user who are members of the current user's group
     * @param $users
     *
     * @return array
     */
    function buddydev_limit_users_search_to_group_members( $users ) {
    	// Do not check for non logged in or the site admin.
    	if ( ! is_user_logged_in() || is_super_admin() ) {
    		return $users;
    	}
    
    	$logged_id = get_current_user_id();
    	$groups = groups_get_user_groups( $logged_id );
    	// If the user does not belong to any group, the result should be empty.
    	if ( empty( $groups['groups'] ) ) {
    		//this user does not belong to any group
    		$users = array();//array( 0, 0 ); // Limit to invalid user.
    		return $users;
    	}
    
    	// if we are here, the user has some groups, let us find out the members of those groups.
    	global $wpdb;
    	$bp = buddypress();
    
    	$list = '(' . join( ',', $groups['groups'] ) . ')';
    
    	$member_ids =  $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id IN {$list} AND is_confirmed = 1 AND is_banned = 0 AND user_id != %d",$logged_id ) );
    
    	if ( ! empty( $member_ids ) ) {
    		$users = array_intersect( $users, $member_ids ); // set intersection.
    	} else {
    		$users = array();
    	}
    
    	return $users;
    }
    add_filter( 'bps_filter_members', 'buddydev_limit_users_search_to_group_members' );
    

    You can put it in your bp-custom.php file.

    This code snippet is provided from BuddyDev.com. I just copied and pasted here as it worked for me.

    Hope you have a good day!

    • This reply was modified 7 years, 12 months ago by Dandy.
    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi @dandyjefferson and @mattrea,

    Sorry I wasn’t able to reply earlier, and thank you Dandy for sharing the solution you found!

    Thread Starter Dandy

    (@dandyjefferson)

    Hi, @dontdream

    No problem. Just hope it will help @mattrea as well.

    Have a good day!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limited Search Range’ is closed to new replies.