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'];
  }

3 comments

    1. Yes, that’ll work (for now), but like I mentioned, the conditions parameter is deprecated and will be removed in Drupal 8.

  1. Thanks! I was just doing the same and thought “this should be easier”; your post helped me realise, yes, it should, but it isn’t, so live with it!

Leave a comment

Your email address will not be published.