How to load a taxonomy term (or entity) by field value? To load a taxonomy term programatically you can use EntityFieldQuery.

&lt;?php

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

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query-&gt;entityCondition('entity_type', 'taxonomy_term')
    -&gt;propertyCondition('vid', $section_vocab-&gt;vid);
  $result = $query-&gt;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.

&lt;?php

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

$sectionID = NULL;

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query-&gt;entityCondition('entity_type', 'taxonomy_term')
    -&gt;propertyCondition('vid', $section_vocab-&gt;vid)
  $result = $query-&gt;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-&gt;field_id['und'][0]['value'];
      if ($value == $IDtoFind){
        $termID = $term-&gt;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?
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-&gt;vid and field_id-&gt;value.

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

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


Using -&gt;fieldCondition('field_id', 'value', $IDtoFind, '=') and -&gt;propertyCondition('vid', $section_vocab-&gt;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. 

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

$sectionID = NULL;

if($section_vocab) {
  $query = new EntityFieldQuery();
  $query-&gt;entityCondition('entity_type', 'taxonomy_term')
    -&gt;fieldCondition('field_id', 'value', $IDtoFind, '=');
  $result = $query-&gt;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']);

  }
}