Drupal - Load term by name
You can use snippet code like by using entityTypeManager :
$term_name = 'Term Name';
$term = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties(['name' => $term_name]);
This functionality appears to be deprecated in Drupal 8.
Use the taxonomy_term_load_multiple_by_name function instead.
Example
<?php
/**
* Utility: find term by name and vid.
* @param null $name
* Term name
* @param null $vid
* Term vid
* @return int
* Term id or 0 if none.
*/
protected function getTidByName($name = NULL, $vid = NULL) {
$properties = [];
if (!empty($name)) {
$properties['name'] = $name;
}
if (!empty($vid)) {
$properties['vid'] = $vid;
}
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
return !empty($term) ? $term->id() : 0;
}
?>
To load a single term ID by term name and vocabulary in Drupal 8 you can use the following snippet.
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
->loadByProperties(['name' => $term_name, 'vid' => 'job_category']);
$term = reset($term);
$term_id = $term->id();