Drupal 7: Load all terms in a Taxonomy vocabulary

There still isn’t a function for this, and the conditions in taxonomy_term_load_multiple have been deprecated, so an EntityFieldQuery is the only (and recommended) way to do this:

  $query = new EntityFieldQuery();
  $result = $query
  ->entityCondition('entity_type', 'taxonomy_term')
  ->propertyCondition('vid', 2, '=') // for vocabulary id 2
  ->execute();

Not too bad, I guess, but it just feels like something that should be doable with a single function call. Maybe I’ll add a wrapper function to a utility module or something:

  function taxonomy_term_get_by_vid($vid) {
    $query = new EntityFieldQuery();
    $result = $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', (int) $vid, '=')
    ->execute();
    return $result['taxonomy_term'];
  }
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Drupal 7: Load all terms in a Taxonomy vocabulary

Leave a Reply

Your email address will not be published. Required fields are marked *