18

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.

4 Answers 4

18

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;
    taxonomy_term_save($term);
    $tid = $term->tid;
  }
  return $tid;
}
1
  • I like its versatility (the else part of it), so it adds the term. Dec 31, 2016 at 22:25
16

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;
node_object_prepare($node);

//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;
node_save($node);
3
  • 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, 2013 at 10:51
  • 2
    Anyone know how to tag node(programatically) using taxonomy termname instead of taxonomy id in drupal 7?
    – Ranjani
    Jun 17, 2013 at 14:32
  • @pretty Use taxonomy_get_term_by_name to get the term object and then use the tid. Oct 27, 2014 at 10:14
2

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();
  $w_node->save();
} 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);
}
1
  • 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, 2016 at 0:52
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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