BP_Groups_Member::get_user_memberships( int $user_id, array $args = array() )

Gets memberships of a user for purposes of a personal data export.

Description

Parameters

$user_id

(Required) ID of the user.

$args

(Optional) Array of optional arguments.

  • 'page'
    (int) Page of memberships being requested. Default 1.
  • 'per_page'
    (int) Memberships to return per page. Default 20.
  • 'type'
    (string) Membership type being requested. Accepts 'membership', 'pending_request', 'pending_received_invitation', 'pending_sent_invitation'. Default 'membership'.

Default value: array()

Return

(array)

Source

File: bp-groups/classes/class-bp-groups-member.php

	public static function get_user_memberships( $user_id, $args = array() ) {
		global $wpdb;

		$bp = buddypress();

		$r = array_merge( array(
			'page'     => 1,
			'per_page' => 20,
			'type'     => 'membership',
		), $args );

		$sql = array(
			'select' => 'SELECT *',
			'from'   => "FROM {$bp->groups->table_name_members}",
			'where'  => '',
			'limits' => '',
		);

		switch ( $r['type'] ) {
			case 'pending_request' :
				return groups_get_requests( array(
					'user_id'  => $user_id,
					'page'     => $r['page'],
					'per_page' => $r['per_page'],
				) );
				break;

			case 'pending_received_invitation' :
				return groups_get_invites( array(
					'user_id'  => $user_id,
					'page'     => $r['page'],
					'per_page' => $r['per_page'],
				) );
				break;

			case 'pending_sent_invitation' :
				return groups_get_invites( array(
					'inviter_id'  => $user_id,
					'page'        => $r['page'],
					'per_page'    => $r['per_page'],
				) );
				break;

			case 'membership' :
			default :
				$sql['where'] = $wpdb->prepare( "user_id = %d AND is_confirmed = 1", $user_id );
				break;
		}

		if ( $r['page'] && $r['per_page'] ) {
			$sql['limits'] = $wpdb->prepare( "LIMIT %d, %d", ( $r['page'] - 1 ) * $r['per_page'], $r['per_page'] );
		}

		$memberships = $wpdb->get_results( "{$sql['select']} {$sql['from']} WHERE {$sql['where']} {$sql['limits']}" );

		foreach ( $memberships as &$membership ) {
			$membership->id           = (int) $membership->id;
			$membership->group_id     = (int) $membership->group_id;
			$membership->user_id      = (int) $membership->user_id;
			$membership->inviter_id   = (int) $membership->inviter_id;
			$membership->is_admin     = (int) $membership->is_admin;
			$membership->is_mod       = (int) $membership->is_mod;
			$membership->is_banned    = (int) $membership->is_banned;
			$membership->is_confirmed = (int) $membership->is_confirmed;
			$membership->invite_sent  = (int) $membership->invite_sent;
		}

		return $memberships;
	}

Changelog

Changelog
Version Description
BuddyPress 4.0.0 BuddyPress 4.0.0
BuddyBoss 1.3.5 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.