2
node_load($nid);
$node->mytaxonomyfield['und'][0]['tid']=$data;
node_save($nid)

my corresponding terms : online,offline,pre-online and the corresponding tid : 1,2,3

my question : what the program will be that changing the field data by words instead of tid

1 Answer 1

3

You can't (at least not directly). Don't forget that multiple taxonomy terms may have the same name so they need to be identified by their tid or you'd get clashes.

You have a couple of options to load a taxonomy term by name, but for each you'll need to know the vid of the term's vocabulary.

The first option is to use the deprecated $conditions parameter of the taxonomy_term_load_multiple() function:

$terms = taxonomy_term_load_multiple(array(), array('name' => 'online', 'vid' => $vid));
$first_term = array_shift($terms);
$tid = $first_term->tid;

As you can see it's limited to loading just one term at a time.

The second (much better) way is to use an EntityFieldQuery:

$terms = array(
  'online',
  'offline',
  'pre-online'
);

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'taxonomy_term')
  ->propertyCondition('vid', $vid)
  ->propertyCondition('name', $terms, 'IN');

$results = $query->execute();

if (isset($results['taxonomy_term'])) {
  // Get an array of tids
  $tids = array_keys($results['taxonomy_term']);
}

That way you can load multiple terms in one go, and take advantage of non-SQL field storage solutions if you need to.

If you're worried about speed you could also go directly into the taxonomy_term table to get this data; but I don't advise doing this unless you really know what you're doing.

1
  • thx a lot,my problem is fixed!
    – cobenash
    Jun 19, 2012 at 8:27

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.