Your cart is currently empty!
Dynamically assign groups
—
by
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
Posted in Groups
Comments
5 responses to “Dynamically assign groups”
Hey Torben,
Sure thing, take your time and let me know if needed.
Cheers
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!
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
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.
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