How to programmatically create forum topics using entity api? I used this post and this post to get started but I was trying to use only the entity API.

Here is my code. So far the node creation works but I'm not able to save the node under the correct tid. 

global $user;
$properties = array(
'type' =&gt; 'forum',
'uid' =&gt; $user-&gt;uid,
'status' =&gt; 0
); 
$entity = entity_create('node', $properties);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper-&gt;title = "My new forum topic";
$terms =  taxonomy_get_term_by_name ('General discussion','forums');

//$wrapper-&gt;taxonomy = $terms; //Doesnt work
//$wrapper-&gt;forum_tid = $terms-&gt;tid; //Doesnt work
//$wrapper-&gt;taxonomy_forums = $terms-&gt;tid; //Doesnt work

$wrapper-&gt;save();
$wrapper-&gt;taxonomy_forums = reset($terms)-&gt;tid; works!! taxonomy_get_term_by_name() returns an array of objects keyed with extra dimension (I think its keyed by the tid). The wrapper's save method will call forum_node_presave($node) but will look in the wrong dimension for the tid if you don't get rid of it.