bp_has_groups( array|string $args = '' )

Start the Groups Template Loop.


Parameters Parameters

$args

(array|string) (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.
  • 'status'
    (array|string) Array or comma-separated list of group statuses to limit results to.
  • '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: ''


Top ↑

Return Return

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


Source 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'] );
		}
	}

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

	// 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' => '',
		'status'             => $status,
		'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' );

	// Setup the Groups template global.
	$groups_template = new BP_Groups_Template( 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'],
		'status'             => $r['status'],
		'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'],
	) );

	/**
	 * Filters whether or not there are groups to iterate over for the groups loop.
	 *
	 * @since 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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
7.0.0 Added $status parameter.
2.7.0 Added $update_admin_cache parameter.
2.6.0 Added $group_type, $group_type__in, and $group_type__not_in parameters.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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