bp_has_members( array|string $args = '' )

Initialize the members loop.


Description 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.


Top ↑

Parameters Parameters

$args

(array|string) (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_ids'
    (array|string|bool) An array or 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 Friends page, defaults to the ID of the displayed user. Otherwise defaults to 0.
  • 'member_type'
    (string|array) Array or comma-separated list of member types to limit results to.
  • 'member_type__in'
    (string|array) Array or comma-separated list of member types to limit results to.
  • 'member_type__not_in'
    (string|array) Array or comma-separated list of member 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.
  • 'xprofile_query'
    (array) Filter results by xprofile data. Requires the xprofile component. See BP_XProfile_Query for details.
  • 'populate_extras'
    (bool) Whether to fetch optional data, such as friend counts. Default: true.

Default value: ''


Top ↑

Return Return

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


Source Source

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

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

	// Default user ID.
	$user_id = 0;

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

	$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'                => 'active',
		'page'                => 1,
		'per_page'            => 20,
		'max'                 => false,

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

		'include'             => false,    // 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_ids'            => false,

		'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.

		'xprofile_query'      => false,
		'populate_extras'     => true      // Fetch usermeta? Friend 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 );

	/**
	 * Filters whether or not BuddyPress has members to iterate over.
	 *
	 * @since 1.2.4
	 * @since 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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Added xprofile_query parameter. Added user_ids parameter.
1.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.