groups_get_group_members( array $args = array() )

Fetch the members of a group.


Description Description

Since BuddyPress 1.8, a procedural wrapper for BP_Group_Member_Query. Previously called BP_Groups_Member::get_all_for_group().

To use the legacy query, filter ‘bp_use_legacy_group_member_query’, returning true.


Parameters Parameters

$args

(Optional) An array of optional arguments.

  • 'group_id'
    (int|array|string) ID of the group to limit results to. Also accepts multiple values either as an array or as a comma-delimited string.
  • 'page'
    (int) Page of results to be queried. Default: 1.
  • 'per_page'
    (int) Number of items to return per page of results. Default: 20.
  • 'max'
    (int) Optional. Max number of items to return.
  • 'exclude'
    (array) Optional. Array of user IDs to exclude.
  • 'exclude_admins_mods'
    (bool|int) True (or 1) to exclude admins and mods from results. Default: 1.
  • 'exclude_banned'
    (bool|int) True (or 1) to exclude banned users from results. Default: 1.
  • 'group_role'
    (array) Optional. Array of group roles to include.
  • 'search_terms'
    (string) Optional. Filter results by a search string.
  • 'type'
    (string) Optional. Sort the order of results. 'last_joined', 'first_joined', or any of the $type params available in BP_User_Query. Default: 'last_joined'.

Default value: array()


Top ↑

Return Return

(false|array) Multi-d array of 'members' list and 'count'.


Top ↑

Source Source

File: bp-groups/bp-groups-functions.php

function groups_get_group_members( $args = array() ) {
	$function_args = func_get_args();

	// Backward compatibility with old method of passing arguments.
	if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
		_deprecated_argument( __METHOD__, '2.0.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );

		$old_args_keys = array(
			0 => 'group_id',
			1 => 'per_page',
			2 => 'page',
			3 => 'exclude_admins_mods',
			4 => 'exclude_banned',
			5 => 'exclude',
			6 => 'group_role',
		);

		$args = bp_core_parse_args_array( $old_args_keys, $function_args );
	}

	$r = bp_parse_args( $args, array(
		'group_id'            => bp_get_current_group_id(),
		'per_page'            => false,
		'page'                => false,
		'exclude_admins_mods' => true,
		'exclude_banned'      => true,
		'exclude'             => false,
		'group_role'          => array(),
		'search_terms'        => false,
		'type'                => 'last_joined',
	), 'groups_get_group_members' );

	// For legacy users. Use of BP_Groups_Member::get_all_for_group() is deprecated.
	if ( apply_filters( 'bp_use_legacy_group_member_query', false, __FUNCTION__, $function_args ) ) {
		$retval = BP_Groups_Member::get_all_for_group( $r['group_id'], $r['per_page'], $r['page'], $r['exclude_admins_mods'], $r['exclude_banned'], $r['exclude'] );
	} else {

		// Both exclude_admins_mods and exclude_banned are legacy arguments.
		// Convert to group_role.
		if ( empty( $r['group_role'] ) ) {
			$r['group_role'] = array( 'member' );

			if ( ! $r['exclude_admins_mods'] ) {
				$r['group_role'][] = 'mod';
				$r['group_role'][] = 'admin';
			}

			if ( ! $r['exclude_banned'] ) {
				$r['group_role'][] = 'banned';
			}
		}

		// Perform the group member query (extends BP_User_Query).
		$members = new BP_Group_Member_Query( array(
			'group_id'       => $r['group_id'],
			'per_page'       => $r['per_page'],
			'page'           => $r['page'],
			'group_role'     => $r['group_role'],
			'exclude'        => $r['exclude'],
			'search_terms'   => $r['search_terms'],
			'type'           => $r['type'],
		) );

		// Structure the return value as expected by the template functions.
		$retval = array(
			'members' => array_values( $members->results ),
			'count'   => $members->total_users,
		);
	}

	return $retval;
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 $group_id now supports multiple values. Only works if legacy query is not in use.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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