BP_Friends_Friendship::search_users( string $filter, int $user_id, int|null $limit = null, int|null $page = null )

Search users.

Description

Parameters

$filter

(Required) String to search by.

$user_id

(Required) A user ID param that is unused.

$limit

(Optional) Max number of records to return.

Default value: null

$page

(Optional) Number of the page to return. Default: false (no pagination - return all results).

Default value: null

Return

(array) $filtered_ids IDs of users who match the query.

Source

File: bp-friends/classes/class-bp-friends-friendship.php

781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
public static function search_users( $filter, $user_id, $limit = null, $page = null ) {
    global $wpdb;
 
    // Only search for matching strings at the beginning of the
    // name (@todo - figure out why this restriction).
    $search_terms_like = bp_esc_like( $filter ) . '%';
 
    $usermeta_table = $wpdb->base_prefix . 'usermeta';
    $users_table    = $wpdb->base_prefix . 'users';
 
    $pag_sql = '';
    if ( !empty( $limit ) && !empty( $page ) )
        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) );
 
    $bp = buddypress();
 
    // Filter the user_ids based on the search criteria.
    if ( bp_is_active( 'xprofile' ) ) {
        $sql = $wpdb->prepare( "SELECT DISTINCT d.user_id as id FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE %s ORDER BY d.value DESC {$pag_sql}", $search_terms_like );
    } else {
        $sql = $wpdb->prepare( "SELECT DISTINCT user_id as id FROM {$usermeta_table} WHERE meta_value LIKE %s ORDER BY d.value DESC {$pag_sql}", $search_terms_like );
    }
 
    $filtered_fids = $wpdb->get_col($sql);
 
    if ( empty( $filtered_fids ) )
        return false;
 
    return $filtered_fids;
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.