friends_update_friend_totals( int $initiator_user_id, int $friend_user_id, string $status = 'add' )

Update user friend counts.

Description

Connection counts are cached in usermeta for performance reasons. After a friendship event (acceptance, deletion), call this function to regenerate the cached values.

Parameters

$initiator_user_id

(int) (Required) ID of the first user.

$friend_user_id

(int) (Required) ID of the second user.

$status

(string) (Optional) The friendship event that's been triggered. 'add' will ++ each user's friend counts, while any other string will --.

Default value: 'add'

Source

File: bp-friends/bp-friends-functions.php

function friends_update_friend_totals( $initiator_user_id, $friend_user_id, $status = 'add' ) {

	if ( 'add' == $status ) {
		bp_update_user_meta( $initiator_user_id, 'total_friend_count', (int)bp_get_user_meta( $initiator_user_id, 'total_friend_count', true ) + 1 );
		bp_update_user_meta( $friend_user_id, 'total_friend_count', (int)bp_get_user_meta( $friend_user_id, 'total_friend_count', true ) + 1 );
	} else {
		bp_update_user_meta( $initiator_user_id, 'total_friend_count', (int)bp_get_user_meta( $initiator_user_id, 'total_friend_count', true ) - 1 );
		bp_update_user_meta( $friend_user_id, 'total_friend_count', (int)bp_get_user_meta( $friend_user_id, 'total_friend_count', true ) - 1 );
	}
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.