BP_REST_Groups_Endpoint::bp_rest_get_sub_groups( integer $parent_group_id )

Get sub groups id.

Description

Parameters

$parent_group_id

(Required) Group ID.

Return

(array)

Source

File: bp-groups/classes/class-bp-rest-groups-endpoint.php

	public function bp_rest_get_sub_groups( $parent_group_id ) {
		if ( empty( $parent_group_id ) ) {
			return array();
		}

		$user_id = bp_loggedin_user_id();
		$filter  = ( false !== $user_id && ! bp_user_can( $user_id, 'bp_moderate' ) );

		if ( function_exists( 'bp_include_group_by_context ' ) ) {
			$sub_groups = groups_get_groups(
				array(
					'parent_id'   => $parent_group_id,
					'fields'      => 'ids',
					'show_hidden' => true,
					'per_page'    => false,
					'page'        => false,
				)
			);

			// Reset parents array to rebuild for next round.
			$groups = array();
			foreach ( $sub_groups['groups'] as $group ) {
				if ( $filter ) {
					if ( bp_include_group_by_context( $group, $user_id, 'normal' ) ) {
						$groups[] = $group->id;
					}
				} else {
					$groups[] = $group->id;
				}
			}

			return $groups;
			// buddypress support.
		} else {
			$sub_groups = groups_get_groups(
				array(
					'parent_id'   => $parent_group_id,
					'fields'      => 'ids',
					'show_hidden' => false,
					'per_page'    => false,
					'page'        => false,
				)
			);

			if ( ! empty( $sub_groups ) && isset( $sub_groups['groups'] ) && ! empty( $sub_groups['groups'] ) ) {
				return $sub_groups['groups'];
			}
		}

		return array();
	}

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.