Skip to:
Content
Pages
Categories
Search
Top
Bottom

Accessing BP_Groups_Member::get_group_ids in Theme Function


  • ToobHed
    Participant

    @toobhed

    I have a smippet of code I am running in my theme’s function.

    $group_ids = BP_Groups_Member::get_group_ids( $user_id );

    When I output the array I always get:
    Array
    (
    [groups] => Array
    (
    )

    [total] => 0
    )
    regardless of the groups for given user. I think I am missing something when trying to use this in functions, as the same code used in “theme\buddypress\members\index.php” will give me the desired results. Any ideas?

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

  • Henry Wright
    Moderator

    @henrywright

    You’re using the class method properly. This should output an array containing an array of groups and a count.

    $group_ids = BP_Groups_Member::get_group_ids( $user_id );
    var_dump( $group_ids );

    Make sure the $user_id argument you are passing to the class method is valid. It should be an integer but I don’t think type is enforced in this case so a string might also work.


    ToobHed
    Participant

    @toobhed

    Thanks for the reply,

    The code runs fine when I call it from “theme\buddypress\members\index.php” and outputs:
    rray(2) { [“groups”]=> array(6) { [0]=> string(1) “2” [1]=> string(1) “0” [2]=> string(2) “11” [3]=> string(1) “7” [4]=> string(2) “12” [5]=> string(2) “13” } [“total”]=> int(6) }

    But when I call it from functions.php in my theme I get the errors:

    Notice: Undefined property: BP_Groups_Component::$table_name_members in /home/vetsgami/public_html/wp-content/plugins/buddypress/bp-groups/classes/class-bp-groups-member.php on line 561

    Notice: Undefined property: BP_Groups_Component::$table_name in /home/myserver/public_html/wp-content/plugins/buddypress/bp-groups/classes/class-bp-groups-member.php on line 561

    Notice: Undefined property: BP_Groups_Component::$table_name_members in /home/myserver/public_html/wp-content/plugins/buddypress/bp-groups/classes/class-bp-groups-member.php on line 562

    Notice: Undefined property: BP_Groups_Component::$table_name in /home/myserver/public_html/wp-content/plugins/buddypress/bp-groups/classes/class-bp-groups-member.php on line 562
    array(2) { [“groups”]=> array(0) { } [“total”]=> int(0) }

    Makes me wonder if I need to include a file from buddypress to run it here?


    ToobHed
    Participant

    @toobhed

    I am trying to call this outside of any buddypress files. I am calling it in my themes functions, it is to run on a cron. I must be missing a global or required file, any ideas?


    Henry Wright
    Moderator

    @henrywright

    You need to hook to something like wp. Try adding this in your functions.php file:

    add_action( 'wp', function() {
        // Add your code here
    } );

    Henry Wright
    Moderator

    @henrywright

    I just noticed you want to schedule your callback. You will need to use the method described here https://codex.wordpress.org/Function_Reference/wp_cron


    ToobHed
    Participant

    @toobhed

    I’m not sure why I would need to hook to something? I am running this process independently as part of a currently working cron job. I want to add a routine to my cron job that would remove users from groups if they are of a certain role. I have everything working, except I am not able to find the groups a user belongs to. I believe the code $group_ids = BP_Groups_Member::get_group_ids( $user_id ); is correct since I get the intended results when I run this from the buddypress page for members. That page must contain all the necessary declarative and required components. When I run the same code in functions.php even supplying a known user id such as $group_ids = BP_Groups_Member::get_group_ids( 2); my array is empty. Furthermore I get the errors noted above that I do not get when running this code from the members page.


    Henry Wright
    Moderator

    @henrywright

    I’m not sure why I would need to hook to something?

    Imagine a scenario where your functions.php file is loaded before something else. That something else won’t be available to you in functions.php.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar