BuddyDev

Search

[Resolved] Limit the searching range of members

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #7970

    Hi, there

    Thanks so much for previous helps and I’m very appreciated about that.

    I have this problem which stuck me for a long time. Currently, I’m using a plugin called – BP Profile Search – https://wordpress.org/plugins/bp-profile-search/. It provides a function which can allow users to search for friends. But by default it’s site-wide searching range.

    Is it possible that after searching, 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.

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #8039

    Hi, there

    Sorry to bother you again about this topic. I’m wondering do you have any suggestions about it?

    Thank you very much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #8214

    Hi Dandy,
    It is doable but i will need the following info:-

    1. How do you determine if a logged in user is a student?
    2. Can a student be part of multiple groups or just one group?

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #8219
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #8284
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #8290
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #8299

    Hi Dandy,
    Please put the following code in your bp-custom.php

    
    /**
     * 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' );
    
    

    It will limit the BP profile search to only look for the Group members.
    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #8346

    Hi, Brajesh

    Sorry to reply you late and I already tested the code snippet above. It worked perfectly and exactly what I want!

    Thanks a million and I will mark it as resolved.

    Thank you very much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #8347

    You are welcome. I am glad I could help.
    Thank you for marking it as resolved 🙂

    Regards
    Brajesh

The topic ‘ [Resolved] Limit the searching range of members’ is closed to new replies.

This topic is: resolved