Search code examples
wordpressbuddypress

Calling a function that adds something to mySQL


I am trying to create some groups in BuddyPress (a wordpress plugin) from a csv file. I have been almost successful doing this but a small part remains.

This is the code I am using:

   <?php

include "../../../wp-load.php";

$groups = array();

if (($handle = fopen("testcsv.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $group = array( 
                'name'        => $data[0],
                'creator_id'  => 1, 
                'description' => $data[1], 
                'slug' => groups_check_slug(sanitize_title(esc_attr($data[2]))), 
                'date_created' => gmdate( "Y-m-d H:i:s" ), 
                'status' => 'public' 

 );
        $groups[] = $group;

    }   
    fclose($handle);
}

foreach ($groups as $group) {
    printf("%s<br>\n", $group);
    groups_create_group($group);
} 

?>

Everything seems fine in the database, but different from adding the groups manually the wp_bp_groupmeta isn't updated with total_member_count 1. I've found the function to update this in wp_groups_functions.php.

// Modify group member count    groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );

My question is, how can I run this function to the correlated group within the code described?


Solution

  • Your call subtracts from the group count. And you need the group_id.

    Try this:

    foreach ($groups as $group) {
        printf("%s<br>\n", $group);
        groups_create_group($group);    
        $group_id = $wpdb->insert_id;
        groups_update_groupmeta( $group_id, 'total_member_count', (int) 1 );
    }