bp_has_members( array|string $args = array() )

Initialize the members loop.

Description

Based on the $args passed, bp_has_members() populates the $members_template global, enabling the use of BuddyPress templates and template functions to display a list of members.

Parameters

$args

(Optional) Arguments for limiting the contents of the members loop. Most arguments are in the same format as BP_User_Query. However, because the format of the arguments accepted here differs in a number of ways, and because bp_has_members() determines some default arguments in a dynamic fashion, we list all accepted arguments here as well. Arguments can be passed as an associative array, or as a URL query string (eg, 'user_id=4&per_page=3').

  • 'type'
    (int) Sort order. Accepts 'active', 'random', 'newest', 'popular', 'online', 'alphabetical'. Default: 'active'.
  • 'page'
    (int|bool) Page of results to display. Default: 1.
  • 'per_page'
    (int|bool) Number of results per page. Default: 20.
  • 'max'
    (int|bool) Maximum number of results to return. Default: false (unlimited).
  • 'page_arg'
    (string) The string used as a query parameter in pagination links. Default: 'bpage'.
  • 'include'
    (array|int|string|bool) Limit results by a list of user IDs. Accepts an array, a single integer, a comma-separated list of IDs, or false (to disable this limiting). Accepts 'active', 'alphabetical', 'newest', or 'random'. Default: false.
  • 'exclude'
    (array|int|string|bool) Exclude users from results by ID. Accepts an array, a single integer, a comma-separated list of IDs, or false (to disable this limiting). Default: false.
  • 'user_id'
    (int) If provided, results are limited to the friends of the specified user. When on a user's Connections page, defaults to the ID of the displayed user. Otherwise defaults to 0.
  • 'member_type'
    (string|array) Array or comma-separated list of profile types to limit results to.
  • 'member_type__in'
    (string|array) Array or comma-separated list of profile types to limit results to.
  • 'member_type__not_in'
    (string|array) Array or comma-separated list of profile types to exclude from results.
  • 'search_terms'
    (string) Limit results by a search term. Default: value of $_REQUEST['members_search'] or $_REQUEST['s'], if present. Otherwise false.
  • 'meta_key'
    (string) Limit results by the presence of a usermeta key. Default: false.
  • 'meta_value'
    (mixed) When used with meta_key, limits results by the a matching usermeta value. Default: false.
  • 'populate_extras'
    (bool) Whether to fetch optional data, such as friend counts. Default: true.

Default value: array()

Return

(bool) Returns true when blogs are found, otherwise false.

Source

File: bp-members/bp-members-template.php

function bp_has_members( $args = array() ) {
	global $members_template;

	// Default user ID.
	$user_id = 0;

	// User filtering.
	if ( bp_is_user_friends() && ! bp_is_user_friend_requests() && ! bp_is_user_mutual_friends() ) {
		$user_id = bp_displayed_user_id();
	}

	$include = false;
	$type = 'active';

	// Mutual User filtering.
	if ( isset( $args['type'] ) && 'online' != $args['type'] && bp_is_user_friends() && bp_is_user_mutual_friends() ) {
		$include = bp_get_mutual_friendships();
		$type = 'alphabetical';
	}

	$member_type = bp_get_current_member_type();
	if ( ! $member_type && ! empty( $_GET['member_type'] ) ) {
		if ( is_array( $_GET['member_type'] ) ) {
			$member_type = $_GET['member_type'];
		} else {
			// Can be a comma-separated list.
			$member_type = explode( ',', $_GET['member_type'] );
		}
	}

	$search_terms_default = null;
	$search_query_arg = bp_core_get_component_search_query_arg( 'members' );
	if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
		$search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
	}

	// Type: active ( default ) | random | newest | popular | online | alphabetical.
	$r = bp_parse_args( $args, array(
		'type'                => $type,
		'page'                => 1,
		'per_page'            => 20,
		'max'                 => false,

		'page_arg'            => 'upage',  // See https://buddypress.trac.wordpress.org/ticket/3679.

		'include'             => $include,    // Pass a user_id or a list (comma-separated or array) of user_ids to only show these users.
		'exclude'             => false,    // Pass a user_id or a list (comma-separated or array) of user_ids to exclude these users.

		'user_id'             => $user_id, // Pass a user_id to only show friends of this user.
		'member_type'         => $member_type,
		'member_type__in'     => '',
		'member_type__not_in' => '',
		'search_terms'        => $search_terms_default,

		'meta_key'            => false,    // Only return users with this usermeta.
		'meta_value'          => false,    // Only return users where the usermeta value matches. Requires meta_key.

		'populate_extras'     => true      // Fetch usermeta? Connection count, last active etc.
	), 'has_members' );

	// Pass a filter if ?s= is set.
	if ( is_null( $r['search_terms'] ) ) {
		if ( !empty( $_REQUEST['s'] ) ) {
			$r['search_terms'] = $_REQUEST['s'];
		} else {
			$r['search_terms'] = false;
		}
	}

	// Set per_page to max if max is larger than per_page.
	if ( !empty( $r['max'] ) && ( $r['per_page'] > $r['max'] ) ) {
		$r['per_page'] = $r['max'];
	}

	// Query for members and populate $members_template global.
	$members_template = new BP_Core_Members_Template(
		$r['type'],
		$r['page'],
		$r['per_page'],
		$r['max'],
		$r['user_id'],
		$r['search_terms'],
		$r['include'],
		$r['populate_extras'],
		$r['exclude'],
		$r['meta_key'],
		$r['meta_value'],
		$r['page_arg'],
		$r['member_type'],
		$r['member_type__in'],
		$r['member_type__not_in']
	);

	/**
	 * Filters whether or not BuddyPress has members to iterate over.
	 *
	 * @since BuddyPress 1.2.4
	 * @since BuddyPress 2.6.0 Added the `$r` parameter
	 *
	 * @param bool  $value            Whether or not there are members to iterate over.
	 * @param array $members_template Populated $members_template global.
	 * @param array $r                Array of arguments passed into the BP_Core_Members_Template class.
	 */
	return apply_filters( 'bp_has_members', $members_template->has_members(), $members_template, $r );
}

Changelog

Changelog
Version Description
BuddyPress 1.2.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.