Posted on

How to show The BuddyPress Activity to Friends Only

BuddyPress loop shows every kind of BuddyPress activity by default. However, the activity loop wasn’t meant to be a one-size-fits-all arrangement. But it is possible that we can filter the loop using a number of specified parameters. Suppose you have a very large BuddyPress sites. In that case user will see a lot of activity in his wall, which is very annoying. Well we can show our activity to friends only. So that user will only see their friends activity. They won’t see everyone activity.

Well, we need to create a bp-custom.php file. bp-custom.php does not exist by default. If you don’t have a file located at /wp-content/plugins/bp-custom.php. Please check here for more details. .

// Let's start !
function bp_friend_only_activity() {
	add_action( 'bp_legacy_theme_ajax_querystring', 'bp_loop_querystring_for_friend_only_activity', 20, 2 );
// Not too early and not too late ! 9 seems ok 😉
add_action( 'bp_include', 'bp_friend_only_activity', 9 );

Do not worry about the bp_legacy_theme_ajax_querystring hook. I will explain about it. We are using bp_include, so that it will load after BuddyPress loading is completed. Inside the bp_friend_only_activity function, we will write our filter function. bp_loop_querystring_for_friend_only_activity is our filter function and  bp_legacy_theme_ajax_querystring is the BuddyPress ajax_querystring hook.


We can write query string  function.

// Show activity to friends only :)  
function bp_loop_querystring_for_friend_only_activity( $query_string, $object ) {
    if ( ! empty( $query_string ) ) {
        $query_string .= '&';

    if ( 'activity' != $object ) // if not in activity page, then return!!

    	// Get your friends
	$friends = friends_get_friend_user_ids( bp_loggedin_user_id() );
	$friends[] = bp_loggedin_user_id();
	$friends_and_me = implode( ',', (array) $friends );
	$friends_and_me =  '&user_id=' . $friends_and_me;
 	$query_string .= $friends_and_me;
    	$query_string .= '&per_page=2'; // You can set pagaination. If not needed, you can remove it.
    	return $query_string;

You can download the code from here: