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.
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?
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?
You need to hook to something like wp
. Try adding this in your functions.php file:
add_action( 'wp', function() {
// Add your code here
} );
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
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.
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.