Monthly Archives: June 2021

Exclude users from search results

If you wish to search your Members directory with BP Profile Search and exclude some users from your search results, you can use one of the following code snippets.

Let’s say you wish to remove the current user from their search results. Add this code to your bp-custom.php:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$donotshow[] = bp_loggedin_user_id ();
	$users = array_diff ($users, $donotshow);

	return $users;
}

If you wish to exclude the administrator(s) from search results, add this code to your bp-custom.php:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$args = array ('role' => 'administrator');
	$admins = get_users ($args);
	$donotshow = wp_list_pluck ($admins, 'ID');
	$users = array_diff ($users, $donotshow);

	return $users;
}

You can even combine the above snippets. The following code excludes both the administrator(s) and the current user from search results:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$args = array ('role' => 'administrator');
	$admins = get_users ($args);
	$donotshow = wp_list_pluck ($admins, 'ID');
	$donotshow[] = bp_loggedin_user_id ();
	$users = array_diff ($users, $donotshow);

	return $users;
}