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!

Comments

Kyle (not verified)

I love what you guys tend to be up too. This kind of clever work and exposure!

Keep up the superb works guys I've included you guys to my personal blogroll.

My website Grand Slam tennis news (www.article.org.in)

Add new comment