bp_has_groups( array|string $args = '' )

Start the Groups Template Loop.

Description

Parameters

$args

(Optional) Array of parameters. All items are optional.

  • 'type'
    (string) Shorthand for certain orderby/order combinations. 'newest', 'active', 'popular', 'alphabetical', 'random'. When present, will override orderby and order params. Default: null.
  • 'order'
    (string) Sort order. 'ASC' or 'DESC'. Default: 'DESC'.
  • 'orderby'
    (string) Property to sort by. 'date_created', 'last_activity', 'total_member_count', 'name', 'random'. Default: 'last_activity'.
  • 'page'
    (int) Page offset of results to return. Default: 1 (first page of results).
  • 'per_page'
    (int) Number of items to return per page of results. Default: 20.
  • 'max'
    (int) Does NOT affect query. May change the reported number of total groups found, but not the actual number of found groups. Default: false.
  • 'show_hidden'
    (bool) Whether to include hidden groups in results. Default: false.
  • 'page_arg'
    (string) Query argument used for pagination. Default: 'grpage'.
  • 'user_id'
    (int) If provided, results will be limited to groups of which the specified user is a member. Default: value of bp_displayed_user_id().
  • 'slug'
    (string) If provided, only the group with the matching slug will be returned. Default: false.
  • 'search_terms'
    (string) If provided, only groups whose names or descriptions match the search terms will be returned. Default: value of $_REQUEST['groups_search'] or $_REQUEST['s'], if present. Otherwise false.
  • 'group_type'
    (array|string) Array or comma-separated list of group types to limit results to.
  • 'group_type__in'
    (array|string) Array or comma-separated list of group types to limit results to.
  • 'group_type__not_in'
    (array|string) Array or comma-separated list of group types that will be excluded from results.
  • 'meta_query'
    (array) An array of meta_query conditions. See WP_Meta_Query::queries for description.
  • 'include'
    (array|string) Array or comma-separated list of group IDs. Results will be limited to groups within the list. Default: false.
  • 'exclude'
    (array|string) Array or comma-separated list of group IDs. Results will exclude the listed groups. Default: false.
  • 'parent_id'
    (array|string) Array or comma-separated list of group IDs. Results will include only child groups of the listed groups. Default: null.
  • 'update_meta_cache'
    (bool) Whether to fetch groupmeta for queried groups. Default: true.
  • 'update_admin_cache'
    (bool) Whether to pre-fetch group admins for queried groups. Defaults to true when on a group directory, where this information is needed in the loop. Otherwise false.

Default value: ''

Return

(bool) True if there are groups to display that match the params

Source

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

function bp_has_groups( $args = '' ) {
	global $groups_template;

	/*
	 * Defaults based on the current page & overridden by parsed $args
	 */
	$slug         = false;
	$type         = '';
	$search_terms = false;

	// When looking your own groups, check for two action variables.
	if ( bp_is_current_action( 'my-groups' ) ) {
		if ( bp_is_action_variable( 'most-popular', 0 ) ) {
			$type = 'popular';
		} elseif ( bp_is_action_variable( 'alphabetically', 0 ) ) {
			$type = 'alphabetical';
		}

	// When looking at invites, set type to invites.
	} elseif ( bp_is_current_action( 'invites' ) ) {
		$type = 'invites';

	// When looking at a single group, set the type and slug.
	} elseif ( bp_get_current_group_slug() ) {
		$type = 'single-group';
		$slug = bp_get_current_group_slug();
	}

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

	// Default search string (too soon to escape here).
	$search_query_arg = bp_core_get_component_search_query_arg( 'groups' );
	if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
		$search_terms = stripslashes( $_REQUEST[ $search_query_arg ] );
	} elseif ( ! empty( $_REQUEST['group-filter-box'] ) ) {
		$search_terms = $_REQUEST['group-filter-box'];
	} elseif ( !empty( $_REQUEST['s'] ) ) {
		$search_terms = $_REQUEST['s'];
	}

	// Parse defaults and requested arguments.
	$r = bp_parse_args( $args, array(
		'type'               => $type,
		'order'              => 'DESC',
		'orderby'            => 'last_activity',
		'page'               => 1,
		'per_page'           => 20,
		'max'                => false,
		'show_hidden'        => false,
		'page_arg'           => 'grpage',
		'user_id'            => bp_displayed_user_id(),
		'slug'               => $slug,
		'search_terms'       => $search_terms,
		'group_type'         => $group_type,
		'group_type__in'     => '',
		'group_type__not_in' => '',
		'meta_query'         => false,
		'include'            => false,
		'exclude'            => false,
		'parent_id'          => null,
		'update_meta_cache'  => true,
		'update_admin_cache' => bp_is_groups_directory() || bp_is_user_groups(),
	), 'has_groups' );

	$args = array(
		'type'               => $r['type'],
		'order'              => $r['order'],
		'orderby'            => $r['orderby'],
		'page'               => (int) $r['page'],
		'per_page'           => (int) $r['per_page'],
		'max'                => (int) $r['max'],
		'show_hidden'        => $r['show_hidden'],
		'page_arg'           => $r['page_arg'],
		'user_id'            => (int) $r['user_id'],
		'slug'               => $r['slug'],
		'search_terms'       => $r['search_terms'],
		'group_type'         => $r['group_type'],
		'group_type__in'     => $r['group_type__in'],
		'group_type__not_in' => $r['group_type__not_in'],
		'meta_query'         => $r['meta_query'],
		'include'            => $r['include'],
		'exclude'            => $r['exclude'],
		'parent_id'          => $r['parent_id'],
		'update_meta_cache'  => (bool) $r['update_meta_cache'],
		'update_admin_cache' => (bool) $r['update_admin_cache'],
	);


	if ( isset( $_POST['template'] ) && 'group_subgroups' === $_POST['template'] ) {
		$descendant_groups   = bp_get_descendent_groups( bp_get_current_group_id(), bp_loggedin_user_id() );
		$ids                 = wp_list_pluck( $descendant_groups, 'id' );
		$args['include']     = $ids;
		$args['slug']        = '';
		$args['type']        = '';
		$args['show_hidden'] = true;
	}

	// Setup the Groups template global.
	$groups_template = new BP_Groups_Template( $args );

	/**
	 * Filters whether or not there are groups to iterate over for the groups loop.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param bool               $value           Whether or not there are groups to iterate over.
	 * @param BP_Groups_Template $groups_template BP_Groups_Template object based on parsed arguments.
	 * @param array              $r               Array of parsed arguments for the query.
	 */
	return apply_filters( 'bp_has_groups', $groups_template->has_groups(), $groups_template, $r );
}

Changelog

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