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

2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
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

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.