BP_Friends_Friendship::total_friend_count( int $user_id )

Get a total friend count for a given user.


Description Description


Parameters Parameters

$user_id

(Optional) ID of the user whose friendships you are counting. Default: displayed user (if any), otherwise logged-in user.


Top ↑

Return Return

(int) Friend count for the user.


Top ↑

Source Source

File: bp-friends/classes/class-bp-friends-friendship.php

	public static function total_friend_count( $user_id = 0 ) {
		global $wpdb;

		if ( empty( $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.
		 */

		$args = array(
			'is_confirmed' => 1,
		);
		$friendships = self::get_friendships( $user_id, $args );
		$count       = count( $friendships );

		// Do not update meta if user has never had friends.
		if ( ! $count && ! bp_get_user_meta( $user_id, 'total_friend_count', true ) ) {
			return 0;
		}

		bp_update_user_meta( $user_id, 'total_friend_count', (int) $count );

		return absint( $count );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.