How to load taxonomy term by name in Drupal 8

Posted by admin on Fri, 01/19/2018 - 14:57

Code snippet to load taxonomy term by name (or any otehr property) in Drupal 8.

$term_name = 'Your Term Name';
$term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $term_name]);

If you want to use vid and name - just add that to properties array like this:

$term_name = 'Your Term Name';
$vid = 3;
$term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $term_name, 'vid' => $vid]);
×

Hey! Don't miss a new cool stuff!

Add new comment