How can I set a term reference field for a field that's limited to one value? I can do it for a term reference field that accepts an unlimited number of values like so...
$entity = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->field_my_term_reference[] = 123;
$wrapper->save();
However, none of the obvious approaches works if the field is limited to one value. Things like the following don't work...
$entity = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->field_my_term_reference = 123;
//$wrapper->field_my_term_reference[0] = 123;
//$wrapper->field_my_term_reference[tid] = 123;
//$wrapper->field_my_term_reference->tid->set(123);
//etc.
$wrapper->save();
Comments
Comment #1
arnoldbird commentedNevermind. I had a mistake in my code. I've confirmed that the following two approaches work when setting a term reference field that accepts only one value...
and
Comment #2
cesar ll commentedI try to update nodes with values of a json in a custom module, the values in taxonomies do not work for me.
$term_name = 'Example';
if term name exists assign else create
I have it like that and it does not work
Please help.
Regards
Comment #3
cesar ll commentedThis was resolved with: