Groups Loop

The site groups loop can be used to output a list of groups created on your site.

Standard Loop Standard Loop

[sourcecode language=”php”]
<?php if ( bp_has_groups() ) : ?>

<div class="pagination">

<div class="pag-count" id="group-dir-count">
<?php bp_groups_pagination_count() ?>
</div>

<div class="pagination-links" id="group-dir-pag">
<?php bp_groups_pagination_links() ?>
</div>

</div>

<ul id="groups-list" class="item-list">
<?php while ( bp_groups() ) : bp_the_group(); ?>

<li>
<div class="item-avatar">
<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar( ‘type=thumb&width=50&height=50’ ) ?></a>
</div>

<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>

<div class="action">
<?php bp_group_join_button() ?>

<div class="meta">
<?php bp_group_type() ?> / <?php bp_group_member_count() ?>
</div>

<?php do_action( ‘bp_directory_groups_actions’ ) ?>
</div>

<div class="clear"></div>
</li>

<?php endwhile; ?>
</ul>

<?php do_action( ‘bp_after_groups_loop’ ) ?>

<?php else: ?>

<div id="message" class="info">
<p><?php _e( ‘There were no groups found.’, ‘buddypress’ ) ?></p>
</div>

<?php endif; ?>
[/sourcecode]

Top ↑

Accepted Parameters Accepted Parameters

The bp_has_groups() function will accept a number of parameters that will manipulate the data being returned.

  • type optional

    Defines the type of groups to return.

    • Accepted arguments: active, newest, popular, random, alphabetical, most-forum-topics, most-forum-posts
    • Default value: active
  • per_page optional

    The number of groups to display on a page before they are paginated to the next page.

    • Default value: 10
  • page optional

    The page to display.

    • Default value: 1
  • max optional

    The total number of groups to return.

    • Default value: false (no limit)
  • user_id optional

    Return only groups that this user is a member of.

    • Default value: false
  • slug optional

    Return only the group that matches this slug

    • Default value: false
  • search_terms optional

    Return only groups that match these search terms

    • Default value: false
  • meta_query optional

    Return only groups that match a meta key and value

    • Default value: false
  • populate_extras optional

    Fetch extra meta for each group such as if the logged in user is a member, or is banned.

    • Default value: true

Top ↑

Example Code to Use Parameters Example Code to Use Parameters

This is the way you can add the various parameters directly:

[sourcecode language=”php”]
bp_has_groups( ‘type=random&max=3&user_id=’ . $user_id )
[/sourcecode]

If you want to use multiple parameters it’s sometimes easier to use an array like this:

[sourcecode language=”php”]
$args = array(
‘type’ => ‘random’,
‘max’ => 3,
‘user_id’ => $user_id
);
if ( bp_has_groups ( $args ) ) { …
[/sourcecode]

If you want to show all groups, regardless of whether the the logged-in or displayed member belongs to them, use this:

[sourcecode language=”php”]
bp_has_groups( ‘user_id=NULL’ )
[/sourcecode]