Skip to main content

Drupal's EntityFieldQuery and Taxonomy

Back-end Development
Drupal

Drupal 7's powerful EntityFieldQuery class can be used as an alternative to writing a view (and using views_get_view_result). When attempting to grab a list of terms in a vocabulary you can do the following.

$taxonomy_query = new EntityFieldQuery;
$taxonomy_query->entityCondition('entity_type', 'taxonomy_term')
  ->propertyCondition('vid', 2)
  ->propertyOrderBy('weight');

$taxonomy_terms = $taxonomy_query->execute();

foreach ($taxonomy_terms['taxonomy_term'] as $tid => $term) {
  # code...
}

I didn't come across any good examples of this the Drupal docs, or anywhere else for that matter.  Hope this helps.

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us