BP_Groups_Member::get_random_groups( int $user_id, int $total_groups = 5 )
Get a list of randomly selected IDs of groups that the member belongs to.
Description
Parameters
- $user_id
-
(Required) ID of the user.
- $total_groups
-
(Optional) Max number of group IDs to return. Default: 5.
Default value: 5
Return
(array) Group IDs.
Source
File: bp-groups/classes/class-bp-groups-member.php
public static function get_random_groups( $user_id = 0, $total_groups = 5 ) { global $wpdb; $bp = buddypress(); // If the user is logged in and viewing their random groups, we can show hidden and private groups. if ( bp_is_my_profile() ) { return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) ); } else { return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) ); } }
Changelog
Version | Description |
---|---|
BuddyPress 1.6.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.