How do I add another field after saving entity metadata wrapper? I'm trying to do something like this:

function create_content($type,$content){
  $node = entity_create('node', array('type' =&gt; $type));
  $emw_node = entity_metadata_wrapper('node', $node);
  $fields = field_info_instances("node",$type);
  foreach ($content as $field =&gt; $value){
    if (array_key_exists($field,$fields)){
      $type=field_info_field_by_id($fields[$field]['field_id'])['type'];
      if ($type=='link_field') {
        $emw_node-&gt;$field-&gt;url = $value;
      }
      else {
        $emw_node-&gt;$field = $value;
      }
    }
  }
  $emw_node-&gt;save();
  $id = $emw_node-&gt;getIdentifier();
  $emw_node-&gt;other_field = $id;
  $emw_node-&gt;save();
}


With the idea of copying the node id to another field. However, I get an error:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. Value at node(9999)->field_other_field: 9999. in EntityMetadataWrapper->set() (line 122 of /sites/all/modules/entity/includes/entity.wrapper.inc).

How do I do this properly, preferably while still using an entity metadata wrapper?
The problem can be with your field format. If your field other_field is a text field check what text format is set for it in admin/structure/types/manage/your_content_type/fields. EG:


If it is different from the plain text format you have to assign an array as a value to your field. EG:

$emw_node-&gt;other_field = array('value' =&gt; $id, 'format' =&gt; 'filtered_html');


or just  

$emw_node-&gt;other_field = array('value' =&gt; $id);