Using Entity metadata wrappers to change field value under hook_node_insert() I am trying to change a field value inside both hook_node_insert() and hook_node_update() using EMW. It actually changes the original $node object that is passed to these hook functions but the change doesn't get saved at the end.

function mymodule_node_insert($node) {
  if (!_is_dynamic_type($node-&gt;type))
    return;

  $messagew = entity_metadata_wrapper('message', message_create('post', array('uid' =&gt; $node-&gt;uid)));

  //copy terms
  $nodew = entity_metadata_wrapper('node', $node);
  //below loads a full taxonomy term
  $nodew-&gt;field_hidden_type = taxonomy_term_machine_name_load('event', 'hidden_type'); 

  $messagew-&gt;field_hidden_type = $nodew-&gt;field_hidden_type-&gt;value();//this actually works
  $messagew-&gt;save();
}


There are a few questions near to this one actually, but none of them addresses this precisely. This one for example is a very good explanation of the mechanism but its suggestion it not use.

Here, one should not use $nodew->save(), because it causes infinite loop. I also played a little with module weight, that does not help either.

I also tried to change another entity reference field. No use.

I don't want to go with field_attach_update() because it is old-fashioned.
The method you are using the save the field isn't ideal.
For setting the field values you should use the set() function provided by the EntityMetadataWrapper class.

function mymodule_node_insert($node) {
  if (!_is_dynamic_type($node-&gt;type))
    return;

  $messagew = entity_metadata_wrapper('message', message_create('post', array('uid' =&gt; $node-&gt;uid)));

  //copy terms
  $nodew = entity_metadata_wrapper('node', $node);
  //below loads a full taxonomy term
  $nodew-&gt;field_hidden_type = taxonomy_term_machine_name_load('event', 'hidden_type'); 

  $messagew-&gt;field_hidden_type-&gt;set($nodew-&gt;field_hidden_type-&gt;value());
  $messagew-&gt;save();
}


On a side note, I see that there is not check on the node type, or any other parameters in the implementation of  hook_node_insert. Note that it will get triggered every time a node of any content type is created.