BP_Friends_Friendship::get_random_friends( int $user_id, int $total_friends = 5 )

Get a list of random friend IDs.


Description Description


Parameters Parameters

$user_id

(Required) ID of the user whose friends are being retrieved.

$total_friends

(Optional) Number of random friends to get. Default: 5.

Default value: 5


Top ↑

Return Return

(array|false) An array of random friend user IDs on success; false if none are found.


Top ↑

Source Source

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

	public static function get_random_friends( $user_id, $total_friends = 5 ) {
		global $wpdb;

		$bp      = buddypress();
		$fids    = array();
		$sql     = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends );
		$results = $wpdb->get_results( $sql );

		for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) {
			$fids[] = ( $results[$i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id;
		}

		// Remove duplicates.
		if ( count( $fids ) > 0 )
			return array_flip( array_flip( $fids ) );
		else
			return false;
	}

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.