Dynamically assign groups

Posted in

Hi George, Kento,

How do you programmatically assign groups so that when a post is submitted from the front end, it is automatically assigned the same group as the submitting user?

I am using Gravity Forms. A user submits a custom post with a form. A hidden field attaches a piece of user meta data which is also the name of the user’s base group.

I can get the id for that group but that’s as far as I get. When I look at the created post, the read group metabox is still empty.

What do I need to do to add the group name after the post is created?

I’m sure there is an easy solution for this…. I’m just not seeing it.
Thanks!
Torben

5 Responses to Dynamically assign groups

  1. George May 15, 2024 at 5:11 pm #

    Hey Torben,

    Sure thing, take your time and let me know if needed.

    Cheers

  2. Torben Berns May 13, 2024 at 11:36 pm #

    Hi George,
    I tried to find where that object would be coming from. I have an idea but so far I have yet to nail it. I’m going to keep trying on my own before I burden you with going through the code. It will take me a couple of days to get a git version set up as well, but I will do that and get back to you. Thanks again!

  3. George May 10, 2024 at 5:09 pm #

    Hey Torben,

    Apologies for the late reply. If you check the line indicated in the warnings, this refers to the function wp_get_attachment_url( $attachment_id = 0 ). I believe that the issue resides in your implementation, perhaps you’re passing an object instead of an id somewhere in your code. If you are able to share it, I might have a better idea of what goes wrong.

    Kind regards,
    George

  4. Torben Berns May 8, 2024 at 9:52 am #

    Hi George,
    Thanks for this! however, something is still not working.
    Here are the relevant lines from wp-debug.log
    [08-May-2024 01:06:10 UTC] PHP Warning: Object of class stdClass could not be converted to int in /ebs2/vhosts/designresearch.ca/designresearch.ca/wp-includes/post.php on line 6600

    [08-May-2024 01:06:45 UTC] Attempting to assign User ID 92 to Group ID 41 (dR-1)
    [08-May-2024 01:06:45 UTC] Group ID Type: integer Value: 41
    [08-May-2024 01:06:45 UTC] Failed to assign User ID 92 to Group ID 41
    [08-May-2024 01:06:49 UTC] PHP Warning: Object of class stdClass could not be converted to int in /ebs2/vhosts/designresearch.ca/designresearch.ca/wp-includes/post.php on line 6600

    Does this tell you where I should be looking for the problem? Thanks again.

  5. George May 7, 2024 at 8:43 am #

    Hey Torben,

    Yes you are correct, there is indeed an easy solution to what you’re looking for. Since you have the group_id and the user_id, all you need to do is execute:

    Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) );

    which is the first snippet in Groups Examples found here: https://docs.itthinx.com/document/groups/api/examples/

    Kind regards,
    George

Only authorized members can comment on this topic.

Log in

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share