Extending BuddyPress Group Hierarchy – Creating a Member Groups list

In this post, I’m going to show you how to create a Member Groups page/list/widget, with a little background on how Group Hierarchy extends the BuddyPress Groups loop.

BuddyPress Groups loop overview

If you aren’t familiar with bp_has_groups() from looking at the BuddyPress theme files, you should take a look at this overview from the BP Codex before continuing.

In a nutshell, bp_has_groups() accepts a series of parameters which it uses to populate the global $groups_template variable with a set of groups. This, in turn, allows all the cool template functions to work without parameters cluttering up your theme file.

How Group Hierarchy changes the Loop

BuddyPress Group Hierarchy changes this in two important ways:

  1. bp_has_groups() creates a set of BP_Groups_Hierarchy objects instead of BP_Groups_Group objects
  2. bp_has_groups_hierarchy() extends the loop with a parent_id parameter for getting only child groups of the passed group ID

There are more changes made just for the Group Tree, but that’s enough to get us started. Onward!

Creating a Member Groups list

Before we build our Member Groups page / widget / list, there’s something you should know about working “in the loop,” i.e. in the context of the current group. The only thing telling BuddyPress what group we’re looking at is the $groups_template variable introduced above. Our member group list is going to change that variable, so we need to save its current state and restore it when we’re done.

// If you're in a function, bring $groups_template into scope
global $groups_template;
// Save the current group context for later
$current_group_template = $groups_template;

Using the Codex article I posted at the top as a reference, you’ll see how easy it is to create a Member Groups page / widget / whatever.

Just replace:

<?php if ( bp_has_groups() ) : ?>


<?php if ( bp_has_groups_hierarchy( array( 
	'type'      => 'alphabetical',
	'parent_id' => bp_get_current_group_id() 
) ) ) : ?>

…and the rest works as is! Since 1.3.0, pagination is handled automatically.

Don’t forget to restore the original context!

$groups_template = $current_group_template;

A complete example

Here’s a quick example that adds a list of Member Groups on the Members page, above the list of users. Drop this in your functions.php file, or in a custom plugin, but note that it will only display if the parent group has at least one member!

add_action( 'bp_before_group_members_content', 'my_func_list_member_groups' );
function my_func_list_member_groups() {
	// If you're in a function, bring $groups_template into scope
	global $groups_template;
	// Save the current group context for later
	$current_group_template = $groups_template;
	if ( bp_has_groups_hierarchy( array( 
		'type'      => 'alphabetical',
		'parent_id' => bp_get_current_group_id(),
	) ) ) : ?>
	<h3><?php _e( 'Member Groups' ); ?><br />&nbsp;</h3>
	<div class="pagination">
		<div class="pag-count" id="group-dir-count">
			<?php bp_groups_pagination_count() ?>
		<div class="pagination-links" id="group-dir-pag">
			<?php bp_groups_pagination_links() ?>
	<ul id="groups-list" class="item-list">
	<?php while ( bp_groups() ) : bp_the_group(); ?>
			<div class="item-avatar">
				<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar( 'type=thumb&width=50&height=50' ) ?></a>
			<div class="item">
				<div class="item-title"><a href="<?php bp_group_permalink() ?>"><?php bp_group_name() ?></a></div>
				<div class="item-meta"><span class="activity"><?php printf( __( 'active %s ago', 'buddypress' ), bp_get_group_last_active() ) ?></span></div>
				<div class="item-desc"><?php bp_group_description_excerpt() ?></div>
				<?php do_action( 'bp_directory_groups_item' ) ?>
			<div class="action">
				<?php bp_group_join_button() ?>
				<div class="meta">
					<?php bp_group_type() ?> / <?php bp_group_member_count() ?>
				<?php do_action( 'bp_directory_groups_actions' ) ?>
			<div class="clear"></div>
	<?php endwhile; ?>
		<?php do_action( 'bp_after_member_groups_loop' ) ?>
	<?php else: ?>
		<div id="message" class="info">
		<p><?php _e( 'There were no member groups found.' ) ?></p>
	<?php endif;
	// restore original group context!
	$groups_template = $current_group_template;

That’s it! Drop me a line in the comments if you find a novel use for this technique.

8 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>