As a follow up:
BP_Groups_Group::check_slug($slug) returns the slug correctly.
I am not sure how, but I managed to get the group id on one occasion, but it’s back to not working again. I was using BP_Groups_Group::get_id_from_slug($slug) when I got the group id.
What you are trying to do is not clear.
A user registers for an Event but how is that Event related to a group?
I have set up an Advanced Custom Field that is linked to the event, and the admin sets the group that the user should be added to on registration for the event. The field is called event_sub_group and it returns a WP_Post object. From that, I can get the slug of the group in the post_name. I was then trying to get the group ID using the slug, so I can add the user to the group. This is my current code.
/Add user to buddyboss group when they register for an event
function ofc_add_registrant_to_group($registration, $old_STS_ID, $new_STS_ID, $context) {
global $bp, $wpdb;
$user_id = get_current_user_id();
if (! $registration instanceof EE_Registration ) {
// No EE_Registration, get out
return;
}
$event_id = $registration->event_ID();
$subgroups = get_field('event_sub_group', $event_id);
$slug = $subgroups[0]->post_name;
$group_id = BP_Groups_Group::group_exists( $slug );
if ( ! groups_is_user_member($user_id, $group_id )) {
$bp->groups->current_group = groups_get_group(array('group_id' => $group_id));
groups_join_group($group_id, $user_id);
}
}
add_action('AHEE__EE_Registration__set_status__to_approved', 'ofc_add_registrant_to_group', 10, 4);
BP_Groups_Group::group_exists( $slug ) returns nothing. If I use BP_Groups_Group::check_slug( $slug ) – it returns the correct slug name as expected.
I don’t know why, but I have found a solution. As ‘check_slug’ worked, I just took the code from that function, amended it to return the group id instead of the slug, and included it in my function – which worked. Be good to understand why none of the other functions worked though?
function ofc_add_registrant_to_group($registration, $old_STS_ID, $new_STS_ID, $context) {
global $wpdb;
$user_id = get_current_user_id();
if (! $registration instanceof EE_Registration ) {
// No EE_Registration, get out
return;
}
$event_id = $registration->event_ID();
$subgroups = get_field('event_sub_group', $event_id);
$slug = $subgroups[0]->post_name;
// These two lines made the difference
$bp = buddypress();
$group_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE slug = %s", $slug ) );
if ( ! groups_is_user_member($user_id, $group_id )) {
groups_join_group($group_id, $user_id);
}
}
add_action('AHEE__EE_Registration__set_status__to_approved', 'ofc_add_registrant_to_group', 10, 4);
What about: BP_Groups_Group::group_exists( $group_slug )
?
/**
* Get whether a group exists for a given slug.
*
* @since BuddyPress 1.6.0
*
* @param string $slug Slug to check.
* @param string|bool $table_name Deprecated.
* @return int|null Group ID if found; null if not.
*/
public static function group_exists( $slug, $table_name = false ) {
global $wpdb;
if ( empty( $slug ) ) {
return false;
}
$args = array(
'slug' => $slug,
'per_page' => 1,
'page' => 1,
'update_meta_cache' => false,
'show_hidden' => true,
);
$groups = self::get( $args );
$group_id = null;
if ( $groups['groups'] ) {
$group_id = current( $groups['groups'] )->id;
}
return $group_id;
}
class-bp-groups-group.php
line 715