Load taxonomy term by name Drupal 8
/**
* Utility: find term by name and vid.
*
* @param string $name
* Term name.
* @param string $vid
* Term vid.
*
* @return int
* Term id, or 0 if none.
*/
public static function getTidByName($name = NULL, $vid = NULL) {
if (empty($name) || empty($vid)) {
return 0;
}
$properties = [
'name' => $name,
'vid' => $vid,
];
$terms = \Drupal::service('entity_type.manager')->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
return !empty($term) ? $term->id() : 0;
}