bp_get_possible_parent_groups( int|bool $group_id = false, int|bool $user_id = false )

Get an array of possible parent group ids for a specific group and user.

Description

To be a candidate for group parenthood, the group cannot be a descendent of this group, and the user must be allowed to create child groups in that group.

Parameters

$group_id

(int|bool) (Optional) ID of the group.

Default value: false

$user_id

(int|bool) (Optional) ID of a user to check group visibility for.

Default value: false

Return

(array) Array of group objects.

Source

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

function bp_get_possible_parent_groups( $group_id = false, $user_id = false ) {
	/*
	 * Passing a group id of 0 would find all top-level groups, which could be
	 * intentional. We only try to find the current group when the $group_id is false.
	 */
	if ( false === $group_id ) {
		$group_id = bp_get_current_group_id();
		if ( ! $group_id ) {
			// If we can't resolve the group_id, don't proceed with a zero value.
			return array();
		}
	}

	if ( false === $user_id ) {
		$user_id = bp_loggedin_user_id();
		if ( ! $user_id ) {
			// If we can't resolve the user_id, don't proceed with a zero value.
			return array();
		}
	}

	// First, get a list of descendants (don't pass a user id--we want them all).
	$descendants = bp_get_descendent_groups( $group_id );
	$exclude_ids = wp_list_pluck( $descendants, 'id' );
	// Also exclude the current group.
	$exclude_ids[] = $group_id;
	$args = [];
	$args = bp_parse_args( $args, array(
		'orderby'         => 'name',
		'order'           => 'ASC',
		'populate_extras' => false,
		'exclude'         => $exclude_ids, // Exclude descendants and this group.
		'show_hidden'     => true,
		'per_page'        => false, // Do not limit the number returned.
		'page'            => false, // Do not limit the number returned.
		'user_id'         => $user_id, // get loggedin users groups
	), 'get_possible_parent_groups'  );

	$possible_parents = groups_get_groups( $args );

	return $possible_parents['groups'];
}

Changelog

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