Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get group ID from slug


  • resortstylebeanbags
    Participant

    @resortstylebeanbags

    I am trying to add a user to a group when they register to an event with event espresso. My code fires when a user successfully registers to the event. I can get the slug (which I assume is the post_name), but I just can’t seem to get the group id from that slug.

    I have tried multiple different ways, but I’m not overly experienced, so would really appreciate some help.

    $slug is successfully set to a string ‘capital-football-c-license-2022’

    Have tried:
    $group_id = BP_Groups_Group::get_id_from_slug($slug); //$group_id is empty
    $group = groups_get_group(array(‘slug’ => $slug)); //$group is empty

    I know the slug is correct as it matches the ‘slug’ field in BP_GROUPS in my database.

    Any help appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)

  • resortstylebeanbags
    Participant

    @resortstylebeanbags

    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.


    shanebp
    Moderator

    @shanebp

    What you are trying to do is not clear.
    A user registers for an Event but how is that Event related to a group?


    resortstylebeanbags
    Participant

    @resortstylebeanbags

    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.


    resortstylebeanbags
    Participant

    @resortstylebeanbags

    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); 
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar