Display Friend Count and Recent Connections in the BuddyPress Sidebar

Display Friend Count and Recent Connections in the BuddyPress Sidebar

connectionsThis is a quick tutorial that utilizes BuddyPress template tags and a filtered activity loop in order to display how many friends a logged-in user has and recent sitewide connections. Opting to show this information in the sidebar will help you to promote connections across your site and increase users’ interaction. This may not be the best way to do this, but it works. If anyone knows an easier or more appropriate way to call this information to the page, please feel free to post in the comments.

The screenshot shows how these items will appear on the page after following the sequence below. I tested this last night with WordPress Single User 2.9.1. and BuddyPress 1.2 and it works fine, but I’m definitely open to hearing more efficient ways to get this done. This tutorial assumes that you are using the default BuddyPress theme with the “Blog” option selected and you’d like to include some friend-specific information in the sidebar on the blog homepage. Obviously, if you have the Activity layout selected in the theme options, you wouldn’t want to include this as a sidebar.

Step 1. Add the function to show how many friends a user has to the functions.php file of your BuddyPress child theme.

{code type=php}
function total_friend_count( $user_id = false ) {
global $wpdb, $bp;

if ( !$user_id )

$user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;

/* This is stored in ‘total_friend_count’ usermeta.

This function will recalculate, update and return. */

$count = $wpdb->get_var( $wpdb->prepare( “SELECT COUNT(id) FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) AND is_confirmed = 1”, $user_id, $user_id ) );

if ( !$count )

return 0;

update_usermeta( $user_id, ‘total_friend_count’, $count );

return $count;


Step 2. Edit sidebar.php to get the total friend count.

Add this before your dynamic sidebar:

{code type=html}

My Total Friends (<?php echo total_friend_count( $user_id = false ) ?>)

Step 3. Include the activity loop in your sidebar.php, filtered to display friend connections.

{code type=php}

<?php if ( bp_has_activities( ‘object=friends’ ) ) : ?>

Recent Connections

      <?php while ( bp_activities() ) : bp_the_activity(); ?>

    • <?php bp_activity_avatar() ?>

      <?php bp_activity_content() ?>

<?php endwhile; ?>

<?php else: ?>

<?php _e(‘There has been no recent site activity.’, ‘buddypress’) ?>

<?php endif;?>

You can find information about how to modify and filter this and other loops to suit your own needs at the BuddyPress codex. There are many optional parameters that you can pass for customization, such as type, the number of users to display before paginating, and the maximum number of users to display. A slew of filters are available for the user ID’s, the various components and their custom actions. Have fun experimenting! Again, if you know of a better way to get the job done, please feel free to post your suggestions for simplification in the comments. If there’s a widget out there for this and I missed it, please let me know. ;)