bp_get_descendent_groups( int|bool $group_id = false, int|bool $user_id = false, string $context = 'normal' )
Get all groups that are descendants of a specific group.
Description
To return all descendent groups, leave the $user_id parameter empty. To return only those child groups visible to a specific user, specify a $user_id.
Parameters
- $group_id
-
(Optional) ID of the group.
Default value: false
- $user_id
-
(Optional) ID of a user to check group visibility for.
Default value: false
- $context
-
(Optional) See bp_include_group_by_context() for description.
Default value: 'normal'
Return
(array) Array of group objects.
Source
File: bp-groups/bp-groups-template.php
function bp_get_descendent_groups( $group_id = false, $user_id = false, $context = 'normal' ) {
/*
* 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();
}
}
// Prepare the return set.
$groups = array();
// If a user ID has been specified, we filter hidden groups accordingly.
$filter = ( false !== $user_id && ! bp_user_can( $user_id, 'bp_moderate' ) );
// Start from the group specified.
$parents = array( $group_id );
$descendants = array();
// We work down the tree until no new children are found.
//while ( $parents ) {
// Fetch all child groups.
$child_args = array(
'parent_id' => $parents,
'show_hidden' => true,
'per_page' => false,
'page' => false,
);
$children = groups_get_groups( $child_args );
// Reset parents array to rebuild for next round.
$parents = array();
foreach ( $children['groups'] as $group ) {
if ( $filter ) {
if ( bp_include_group_by_context( $group, $user_id, $context ) ) {
$groups[] = $group;
$parents[] = $group->id;
}
} else {
$groups[] = $group;
$parents[] = $group->id;
}
}
//}
return $groups;
}
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.