BuddyPress

BuddyPress: The Lowdown on Loop Filtering

BuddyPress has default components that add extra social functionality to a WordPress install. Sometimes you will want to customize what each component displays and that is done through loop filtering. Continue reading if you want to learn more about customizing the output of BuddyPress loops.

Core Components

  • Activity Streams
  • Groups
  • Extended Profiles
  • Private Messaging
  • Notifications
  • Friend connections
  • Account Settings
  • Blogs component (multi-site activated)

The lists of content in these components are displayed using loops. BuddyPress loops are very similar to WordPress post loops. The loops can be manipulated by passing arguments the same as you would do to alter the returned posts in WordPress post loop.

<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) ) ) : ?>
    <?php while ( bp_activities() ) : bp_the_activity(); ?>
 
        <?php locate_template( array( 'activity/entry.php' ), true, false ); ?>
 
    <?php endwhile; ?>
<?php endif; ?>

In the past, it was difficult to filter BuddyPress loops and most of the time people would edit the template files and hard code arguments directly to the loop, cutting off the ability to dynamically filter. Rejoice, with the inclusion of bp_parse_args() filter the loops are easily filtered with minimal effort. Let’s go in-depth with the Activity loop. Other BuddyPress loops are filtered in the same manner but their arguments may differ.

The activity loop starts off with bp_has_activities(). The prefix is bp_ so that leaves has_activities. The other loops are similar; has_groups, has_message_threads etc. We need has_activities to use along with bp_parse_args() to target the activity loop. There are two places to filter a loop. Before arguments are merged with defaults and after.

  • bp_before_{$filter_key}_parse_args – Allows you to filter the initial arguments, $args.
  • bp_after_{$filter_key}_parse_args – Allows you to filter the arguments after $args has been merged with $defaults.

{$filter_key} is where you supply what loop you want the filter to run on. Since we are looking at the Activity Stream we want to use has_activities so that would make our filter be bp_after_has_activities_parse_args.

Filter Activity

Default Activity Loop argument per_page is twenty items per page. Here is an example function filtering bp_after_has_activities_parse_args to change the default to five items per page. bp_is_user_activity() is a check to only run this on a member’s activity loop and not the main site-wide activity loop.

function my_filter_activity( $loop ) {

    // only fetch the last five entries if we're on a user's activity page
    if ( bp_is_user_activity() ) {
        $loop['per_page'] = 5;
    }

    return $loop;
}
add_filter( 'bp_after_has_activities_parse_args', 'my_filter_activity' );

The Codex page for Activity loop lists the available arguments to pass the activity loop. You can include multiple arguments in the bp_after_has_activities_parse_args filter. In this filter, we are filtering the activity loop to show only five items that the logged in user has posted in groups.

function my_filter_activity( $loop ) {

    $loop['per_page'] = 5;
    $loop['scope'] = 'just-me';
    $loop['object'] = 'groups';
    
    return $loop;
}
add_filter( 'bp_after_has_activities_parse_args', 'my_filter_activity' );

Thanks to bp_parse_args(), it makes it trivial to filter or limit content. One usage example of passing per_page to filter is to limit content for none paid members. Many BuddyPress sites are paid membership sites. You could limit the amount of messages displayed unless the user has a certain membership level. Or you may want to clean up the site-wide activity stream to remove the noise.

The biggest request over the years with the activity stream is to have the lauded Facebook-style news feed, with the activity of you and your friends only. This required crazy code in the past to remove everything but those specific activity items. In recent BuddyPress, we can pass multiple scopes–this, along with bp_parse_args(), you can use $loop[‘scope’] = ‘just-me,friends’ in the filter function and BOOM! You’ve got a Facebook-style news feed.

function my_filter_activity( $loop ) {
    
    $loop['scope'] = 'just-me,friends';
 
    return $loop;
}
add_filter( 'bp_after_has_activities_parse_args', 'my_filter_activity' );

Hope this post helps you tame BuddyPress loops! Comment below if you have any questions. To learn more about BuddyPress loops and what arguments you can pass to filter, head over to the Codex on BuddyPress.org!

Comments

7 thoughts on “BuddyPress: The Lowdown on Loop Filtering

    1. A question though, How to I target an activity loop for a custom profile tab that I have created ? This code seems to affect evrything based on the hook

      1. Hi Tafmakura. Could you please give me more details how to use bp_is_current_action( ‘mentions’ )?

  1. I want to create a filter in activity loop for a custom post type – events manager event type. Is there a way to filter the activity loop to list only the events for which the user profile has the same attribute – for instance filter events which have a category = running if the user has a sport = running in profile

  2. Hi..

    I’m new to all this, I don’t understand much. But I want to filter out everything except what eacher user posts.

    Where do I write this function in?

    in the activity-loop.php after

    this?

    and then upload this php file into my child theme?

Have a comment?

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

accessibilityadminaggregationanchorarrow-rightattach-iconbackupsblogbookmarksbuddypresscachingcalendarcaret-downcartunifiedcouponcrediblecredit-cardcustommigrationdesigndevecomfriendsgallerygoodgroupsgrowthhostingideasinternationalizationiphoneloyaltymailmaphealthmessagingArtboard 1migrationsmultiple-sourcesmultisitenewsnotificationsperformancephonepluginprofilesresearcharrowscalablescrapingsecuresecureseosharearrowarrowsourcestreamsupporttwitchunifiedupdatesvaultwebsitewordpress