1

To load a taxonomy term programatically you can use EntityFieldQuery.

<?php

// Get taxonomy term by field value
$section_vocab = taxonomy_vocabulary_machine_name_load('section');

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', $section_vocab->vid);
  $result = $query->execute();

  ...

}

However, that doesn't load the taxonomy term fields. Adding a field condition to the query$query->fieldCondition('field_id', 'value', '123'); to the query doesn't work as the result is always empty.

Bad solution

So the solution using EntityFieldQuery that I found here https://www.drupal.org/node/1517744 works, but it requires looping through all the terms in the vocabulary (in the worst case) which has a high cost.

<?php

// Get taxonomy term by field value
$section_vocab = taxonomy_vocabulary_machine_name_load('section');

$sectionID = NULL;

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', $section_vocab->vid)
  $result = $query->execute();

  if (!empty($result['taxonomy_term'])) {
    // To load all terms.
    $terms = taxonomy_term_load_multiple(array_keys($result['taxonomy_term']));

    // Look up for the first term with the field value that we want.
    foreach ($terms as $term) {
      // Stop looking if we find it
      $value = $term->field_id['und'][0]['value'];
      if ($value == $IDtoFind){
        $termID = $term->tid;
        break;
      }
    }

  }
}

If the taxonomy term has a field with machine field_id, how can you load the taxonomy terms with a specific value for it?

7
  • There is some documentation about loading taxonomy terms using EntityFieldQuery in drupal.org/node/1517744.
    – Miguelos
    Commented Feb 6, 2015 at 10:17
  • As that page should make clear, after you run the query you have to load the entities in a separate step. EntityFieldQuery is effectively an SQL query that just returns shell objects for the entities. The fieldCondition() function adds WHERE clauses to the query - it doesn't affect the type of data returned. To get the actual field data you have to load the entities using entity_load_multiple or an equivalent. Commented Feb 6, 2015 at 10:31
  • @AlfredArmstrong thanks. I am looking for a query that can load only the items with a specific field value without having to loop over every term in the vocabulary. I understand that with EntityFieldQuery is not possible to do that. Is there a way to do it, not building a custom query?
    – Miguelos
    Commented Feb 6, 2015 at 10:44
  • If you use a field condition it does exactly that. What makes you think it loops over the data? It generates an SQL query with the conditions you add turned into WHERE clauses, so it's as efficient as a custom query in most cases. Commented Feb 6, 2015 at 13:45
  • @AlfredArmstrong I added the solution I have right now. I couldn't use fieldCondition in the query and have to loop over each term in the vocabulary.
    – Miguelos
    Commented Feb 6, 2015 at 15:18

2 Answers 2

2

As specified in the EntityFieldQuery documentation. It is not possible to query across multiple entity types. It is no possible to query all the terms with a specific field value as this would require querying both vocabulary->vid and field_id->value.

To load a taxonomy term by field value is possible to get all the entities that have that value:

  $query->entityCondition('entity_type', 'taxonomy_term')
    ->fieldCondition('field_id', 'value', $IDtoFind, '=');

Using ->fieldCondition('field_id', 'value', $IDtoFind, '=') and ->propertyCondition('vid', $section_vocab->vid) in the same query would produce an empty result.

Solution

For the code in the question this would work, based on that field_id is used only one vocabulary and it value is unique.

<?php
// Get taxonomy term by field value
$section_vocab = taxonomy_vocabulary_machine_name_load('section');

$sectionID = NULL;

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term')
    ->fieldCondition('field_id', 'value', $IDtoFind, '=');
  $result = $query->execute();

  if (!empty($result['taxonomy_term'])) {
    // Set the internal pointer of an array to its first element
    reset($result['taxonomy_term']);
    // Get the key value for the first item
    $termID = key($result['taxonomy_term']);

  }
}
1

You have to use https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_tree/7

In your case you have to set "load_entities" to TRUE

taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
1
  • 1
    The issue is this loads all the terms in the vocabulary and then I've to find the one whit the value I'm looking for which takes a lot of time as I have to run this code for every node.
    – Miguelos
    Commented Feb 6, 2015 at 15:41

Your Answer

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

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