I created taxonomy terms and assigned it to a content type 'Article' using term reference field. I created nodes by programmatically but how to set taxonomy terms for the nodes?

The taxonomy terms are created already. I want to set the taxonomy term for a node programmatically.


Use this function to get ID from Termname

function _get_tid_from_term_name($term_name) {
  $vocabulary = 'tags';
  $arr_terms = taxonomy_get_term_by_name($term_name, $vocabulary);
  if (!empty($arr_terms)) {
    $arr_terms = array_values($arr_terms);
    $tid = $arr_terms[0]->tid;
   else {
    $vobj = taxonomy_vocabulary_machine_name_load($vocabulary);
    $term = new stdClass();
    $term->name = $term_name;
    $term->vid = $vobj->vid;
    $tid = $term->tid;
  return $tid;
| improve this answer | |
  • I like its versatility (the else part of it), so it adds the term. – Eduardo Chongkan Dec 31 '16 at 22:25

When you creating a node programmatically, you can assign it to any term like this:

$node = new stdClass();
$node->type = 'YOUR_CONTENT_TYPE_NAME';
$node->title = 'title';

$node->language = LANGUAGE_NONE;

//id of your taxonomy term
$tid = 1;

//add term to a node field
//field_yourfield_name - machine name of your term reference field

$node->field_yourfield_name[$node->language][0]['tid'] = $tid;
| improve this answer | |
  • 1
    @neok.Thanks.It works well using term id.But to how to set termnames? is that like $node->field_yourfield_name[$node->language][0]['name'] = $termname; – Ranjani Jun 15 '13 at 10:51
  • 2
    Anyone know how to tag node(programatically) using taxonomy termname instead of taxonomy id in drupal 7? – Ranjani Jun 17 '13 at 14:32
  • @pretty Use taxonomy_get_term_by_name to get the term object and then use the tid. – Lee Woodman Oct 27 '14 at 10:14

You can use some entity metadata wrappers to achieve that, e.g.:

try {
  $w_node = entity_metadata_wrapper('node', $entity);
  $w_some_term = entity_metadata_wrapper('taxonomy_term', 1234);
  $w_node->field_term_ref = $w_some_term->tid->value();
} catch (Exception $e) {
  drupal_set_message(t('Error for node: @title, message: @error.',
        array('@title' => $w_node->title->value(), '@error' => $e->getMessage())), 'error');
  watchdog_exception('foo', $e);
| improve this answer | |
  • 1
    +1 for using EMWs. They also have a handy method getIdentifier(), which is one less step than $w_some_term->tid->value(). Here's a great article that helped me understand this concept so much better, and to which I refer regularly when using EMWs: – scotself Jul 6 '16 at 0:52

When adding terms, I found I needed to add $term->parent = array(0) otherwise the taxonomy_term_hierarchy table isn't updated properly, and the terms don't show in the admin list page for that vocabulary

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.