Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/02cadc8439c90d240ed54c84e7327aaa to your computer and use it in GitHub Desktop.
Save ipokkel/02cadc8439c90d240ed54c84e7327aaa to your computer and use it in GitHub Desktop.
Helper function to retrieve the group parent User ID of the latest group account a user subscribed to.
<?php
/**
* Helper function to get the parent ID of a group account for a given user.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprogroupacct_get_group_parent_id( $user_id ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
// Find group member entries for this user, sorted by ID in descending order to get the latest first
$group_members = PMProGroupAcct_Group_Member::get_group_members(
array(
'group_child_user_id' => $user_id,
'group_child_status' => 'active',
'orderby' => '`id` DESC', // Get the most recent membership first
)
);
if ( empty( $group_members ) ) {
return null; // User is not a group member.
}
$group_member = $group_members[0]; // Get the latest active group membership.
$group_id = $group_member->group_id; // Get the group ID.
$group = new PMProGroupAcct_Group( $group_id ); // Get the group object.
$group_parent_user_id = $group->group_parent_user_id; // Get the group parent user ID.
return $group_parent_user_id; // Return the group parent user ID.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment