Title: Recursive function
Last modified: August 21, 2016

---

# Recursive function

 *  [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * (@laurahreniucu)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recursive-function/)
 * Hello.
 * I was wondering if i could get some help. I want to create a function to count
   all the unread documents in a group.
 * I already did that for members, but now i’m struggling with this one. This is
   my approach:
 *     ```
       function bp_docs_get_unread_count_group($user_id = false, $group_id) {
       	if ( false === $user_id ) {
       		$user_id = bp_loggedin_user_id();
       	}
   
       	$unread_post_count = 0;
       	if (bp_group_hierarchy_has_subgroups()) {
       		// recursively call bp_docs_get_unread_count_group for every child
       	} else {
       		// for every member in the group call bp_docs_get_unread_count and add all the counts
       	}
   
       	if( $unread_post_count == 0) {
       		$unread_post_count = '';
       	}
   
       	return $unread_post_count;
   
       }
       ```
   
 * Can anyone help me?
 * [http://wordpress.org/plugins/bp-group-hierarchy/](http://wordpress.org/plugins/bp-group-hierarchy/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [David Dean](https://wordpress.org/support/users/ddean/)
 * (@ddean)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recursive-function/#post-4172591)
 * Hi laurahreniucu –
 * You can use this method call in place of `bp_group_hierarchy_has_subgroups()`
   to get a list of the immediate children:
 * `BP_Groups_Hierarchy::has_children( $group_id )`
 * – David
 *  Thread Starter [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * (@laurahreniucu)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/recursive-function/#post-4172609)
 * Thank you David Dean for the response.
 * I’m actually struggling with the recursive function. I can’t seem to make it 
   work.
 * This is my code so far (the ‘else’ is just a dummy for now):
 *     ```
       function bp_docs_get_unread_count_group($user_id = false, $group_id) {
       	if ( false === $user_id ) {
       		$user_id = bp_loggedin_user_id();
       	}
   
       	$unread_post_count = 0;
       	$children = BP_Groups_Hierarchy::has_children( $group_id );
   
       	if($children.length > 0) {
       		foreach($children as $child) {
       			$unread_post_count = $unread_post_count + bp_docs_get_unread_count_group($user_id,$group_id);
       		}
       	} else {
       		$unread_post_count = 1;
       	}
   
       	if( $unread_post_count == 0) {
       		$unread_post_count = '';
       	}
   
       	return $unread_post_count;
   
       }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Recursive function’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/bp-group-hierarchy.svg)
 * [BP Group Hierarchy](https://wordpress.org/plugins/bp-group-hierarchy/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/bp-group-hierarchy/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/bp-group-hierarchy/)
 * [Active Topics](https://wordpress.org/support/plugin/bp-group-hierarchy/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/bp-group-hierarchy/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/bp-group-hierarchy/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [laurahreniucu](https://wordpress.org/support/users/laurahreniucu/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/recursive-function/#post-4172609)
 * Status: not a support question