Skip to:
Content
Pages
Categories
Search
Top
Bottom

Weird double query using WPDB

  • Avatar of Marcella
    Marcella
    Participant

    @marcella1981

    Hey guys,
    I’m running a query whenever someone logs in for the first time to sign them up to some groups automatically.

    Here is the code.

    foreach($officialGroups as $groupSubscribe) :

    $subscription = $wpdb->insert(‘wp_bp_groups_members’,
    array(
    ‘group_id’ => $groupSubscribe, ‘user_id’ => 5, ‘inviter_id’ => 0,
    ‘is_admin’ => 0, ‘is_mod’ => 0, ‘user_title’ => ‘Member’,
    ‘date_modified’ => “$time”, ‘comments’ => ”,
    ‘is_confirmed’ => 1, ‘is_banned’ => 0, ‘invite_sent’ => 0),
    array(
    ‘%d’, ‘%d’, ‘%d’,
    ‘%d’, ‘%d’, ‘%s’,
    ‘%s’, ‘%s’,
    ‘%d’, ‘%d’, ‘%d’));
    if($subscription)
    {
    echo ‘Query ran
    ‘;
    }

    endforeach;

    It’s being problematic. The $officalGroups array is simply a list of group ids being passed over. There are 6 in total.

    I’ve done an echo at each iteration of the loop and it confirms the query only runs once, however when I check the DB and consequently the front-end it’s subsribed te member to each group 3 times.

    Any ideas why?

    I’ve tried running 6 manual queries, flushing the last WPDB transaction and so on but nothing seems to work.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Avatar of Travel-Junkie
    Boris
    Participant

    @travel-junkie

    Why don’t you use BP internal functions, like groups_join_group() or use the class BP_Groups_Member? To what action hooks have you attached your function?

    Avatar of Marcella
    Marcella
    Participant

    @marcella1981

    All i’ve done here is when the user is logged in and when the user matches my own custom login counter at 0 they are then signed up to these groups.

    I don’t think the BP internal functions keep track of login counts?

    It would be inefficient to run this every time they login.

    Avatar of Travel-Junkie
    Boris
    Participant

    @travel-junkie

    Didn’t say that, but you said you’re having trouble with a query, so I suggested that you use the function groups_join_group to sign the user up to your groups. Just substitute the query in your code above with that function. Alternatively, if you don’t want an activity entry (which you get with this function), you can use the BP_Groups_Member::save() method to add a user to a group. You can then do the login count with usermeta.

    Avatar of Marcella
    Marcella
    Participant

    @marcella1981

    Thanks for that, i hadn’t clicked at your clues above. Will post back the results.

    Cheers

    Avatar of Marcella
    Marcella
    Participant

    @marcella1981

    Any ideas how to use this function, there is no documentation on accepted parameters. Nor is it listed in the function reference guide?

    Thanks again

    *i still wonder why it’s running 3 times, it works fine minus being entered 3 times.

    Avatar of Travel-Junkie
    Boris
    Participant

    @travel-junkie

    That’d be
    `groups_join_group( $group_id, $user_id );`

    As for the class, you instantiate it, set all the variables and then call the save() method.

    I was asking for the action hooks before. Maybe that’s why it runs 3 times…

    You definitely shouldn’t do direct SQL queries to add group members, like @travel-junkie says.

    Avatar of Marcella
    Marcella
    Participant

    @marcella1981

    Thanks guys, upon inspecting a plugin with similar functionality then finding one with the exact functionality came accross this code.
    $new_member = new BP_Groups_Member;
    $new_member->group_id = $group_id;
    $new_member->inviter_id = 0;
    $new_member->user_id = $user_id;
    $new_member->is_admin = 0;
    $new_member->user_title = ”;
    $new_member->date_modified = time();
    $new_member->is_confirmed = 1;

    Which is a good insight of how to use a BP class in the future.

    There were no actions being called on the WPDB query, would that be a reason why?

    Unrelated note here, wouldn’t it be wise to advise plugin devs who utilise jQuery and default UI theme to rename their CSS class selectors as it always annoyingly clashes.

    It’s either that or all theme developers have to rename theirs. A duel *slap :p

    Thanks for help

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

You must be logged in to reply to this topic.