BP_Core_User::search_users( string $search_terms, int|null $limit = null, int $page = 1, boolean $populate_extras = true )

Find users who match on the value of an xprofile data.

Description

Parameters

$search_terms

(string) (Required) The terms to search the profile table value column for.

$limit

(int|null) (Optional) The limit of results we want.

Default value: null

$page

(int) (Optional) The page we are on for pagination.

Default value: 1

$populate_extras

(boolean) (Optional) If we should populate extra user fields.

Default value: true

Return

(array) Associative array.

Source

File: bp-core/classes/class-bp-core-user.php

	public static function search_users( $search_terms, $limit = null, $page = 1, $populate_extras = true ) {
		global $wpdb;

		$bp = buddypress();

		$user_ids = array();
		$pag_sql  = $limit && $page ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) ) : '';

		$search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';
		$status_sql        = bp_core_get_status_sql( 'u.' );

		/**
		 * Filters the SQL used to query for searched users count.
		 *
		 * @since BuddyPress 1.0.0
		 *
		 * @param string $value SQL statement for the searched users count query.
		 */
		$total_users_sql = apply_filters( 'bp_core_search_users_count_sql', $wpdb->prepare( "SELECT COUNT(DISTINCT u.ID) as id FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC", $search_terms_like ), $search_terms );

		/**
		 * Filters the SQL used to query for searched users.
		 *
		 * @since BuddyPress 1.0.0
		 *
		 * @param string $value SQL statement for the searched users query.
		 */
		$paged_users_sql = apply_filters( 'bp_core_search_users_sql',       $wpdb->prepare( "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.user_email FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC{$pag_sql}", $search_terms_like ), $search_terms, $pag_sql );

		$total_users = $wpdb->get_var( $total_users_sql );
		$paged_users = $wpdb->get_results( $paged_users_sql );

		/**
		 * Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list.
		 * We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join)
		 */
		foreach ( (array) $paged_users as $user )
			$user_ids[] = $user->id;

		// Add additional data to the returned results.
		if ( $populate_extras )
			$paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids );

		return array( 'users' => $paged_users, 'total' => $total_users );
	}

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.