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!