how to programmatically set a group audience I am trying to programmatically add the group audience widget to a custom form (without using OG field setting) and set it to a default value. 

Would anyone know of a way to do so?



Well after one week of investigation i still haven t find an answer. I gotta say i am really dispointed by OG module, the documentation is so poor and the maintainer doesn't really help improving this. A real shame such a module isn't more documented
First in your .install when creating your content type, add:

og_create_field(OG_AUDIENCE_FIELD, 'node', 'bundle_name_of_your_content_type');


Then when you need to assign a fresh new content of your content type to an existing group, you can do it in two ways:

1) Found it http://drupal.org/node/1249396#comment-4885442 and thanks to tenken

$node = node_load($nid);  
$values = array(  
  'entity type' =&gt; 'node',  
  'entity' =&gt; $node, 
  'state' =&gt; OG_STATE_ACTIVE);
og_group($gid, $values);
node_save($node);


2) Maybe less sexy

$node = node_load($nid);
$node-&gt;group_audience['und'][0] = array(
  'gid' =&gt; $gid, 
  'state' =&gt; OG_STATE_ACTIVE, 
  'created' =&gt; mktime());
node_save($node);


Cheers