How would you programmatically create an OG group? I have tried node_save() but to no joy.

  • 1
    Which Drupal version? – marcvangend Jun 21 '11 at 20:37
  • 6. Tag added to question. – Lucy Jun 21 '11 at 21:18
  • Hmmm... looking at the og_nodeapi function, it looks like node_save should do the trick. I can't see why it doesn't work. Just a thought: maybe you can try building a $form_state array and run drupal_execute. – marcvangend Jun 21 '11 at 23:24

It's simpler than attaching node as group post:

  global $user;
  $node = new stdClass();
  $node->type = 'group'; // Change this to group node type name.
  $node->uid = $user->uid;
  $node->title = t('Title'); // Change title generation
  $node->body = t('Body'); // Change body generation
  $node->og_description = t('Group description'); // Change group description generation
  $node->og_selective = OG_CLOSED;
  $node->og_register = OG_REGISTRATION_NEVER;
  $node->og_directory = OG_DIRECTORY_CHOOSE_FALSE;
  $node->og_private = 0;
  og_save_subscription($node->nid, $node->uid, array('is_active' => 1, 'is_admin' => 1));

(ofcourse you also need to check the return values from alle the three functions)

| improve this answer | |

I don't really understand the question, but I created a lot of groups using the Mass Create module.

| improve this answer | |
  • well laxman13 and Reg Gordon - thx for the answer. I try to do it like you. I come back and report all my findings! – zero Mar 27 '12 at 15:34

I was in the same struggle, but I realized that to create a new OG group programatically, the Rules module could be the key.

If you look for the right trigger and conditions, the first action would be "Entities > Create a new entity >OG group.

You can use the Rules scheduler to do the timing, if necessary.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.