Skip to:
Content
Pages
Categories
Search
Top
Bottom

altering the user search query


  • louie171
    Participant

    @louie171

    hi

    On the members directory page, I’d like to be able to alter the query to search members by group.

    e.g. if I put group name in the text search, I want to return all the members in that group.

    I’m fine with the sql part, but don’t know where to start to hook/filter into to add the sql. Can anyone point me in the right direction?

Viewing 3 replies - 1 through 3 (of 3 total)

  • louie171
    Participant

    @louie171

    ok, after abit of digging I found away that might work.

    Buddypress seems to WP_User_Query for the members query, so I can filter on pre_get_users

    what I’m going todo is run a query where to find all users what are in a group that search term matches e.g. (pseudo query haven’t looked at the tables yet)

    select ids_of_users from users, groups
    where group_name like ‘%SEARCH_TERM%’ (join users and groups)

    Then from the ids above I can do something like this:

    $query->set( ‘include’, (array) $user_ids );

    looks like that should work, but welcome any input.


    louie171
    Participant

    @louie171

    update: I wrote the code. It all seems to work fine (except), it doesn’t seem to affect the query (i.e. the results in members index page are not right). Not sure why ?

    Any input would be hugely appreciated:

    function add_groups_to_members_search(  $query_args  ) {
        
        $search = esc_sql($_REQUEST['search_terms']);  // search box on members index page
        
        global $wpdb;
        
        // get users in groups that search term is like
        $sql = "SELECT m.user_id FROM wp_bp_groups g, wp_bp_groups_members m where g.id = m.group_id and g.name like '%$search%'"; 
        
        $myrows = $wpdb->get_results( $sql );
           
        $user_ids = array();
        
        foreach ($myrows as $obj ) {        
            $user_ids[] = $obj->user_id;      
        }
        
        $query_args['include'] = $user_ids;
        
        return $query_args;
        
    }
    
    add_filter( 'bp_before_has_members_parse_args', 'add_groups_to_members_search', 999999, 1 );

    Varun Dubey
    Participant

    @vapvarun

    @louie171 You can use a standard loop instead of direct SQL queries

    Group Members Loop

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar