4.6.x taxonomy.module taxonomy_get_term_by_name($name)
4.7.x taxonomy.module taxonomy_get_term_by_name($name)
5.x taxonomy.module taxonomy_get_term_by_name($name)
6.x taxonomy.module taxonomy_get_term_by_name($name)
7.x taxonomy.module taxonomy_get_term_by_name($name, $vocabulary = NULL)

Try to map a string to an existing term, as for glossary use.

Provides a case-insensitive and trimmed mapping, to maximize the likelihood of a successful match.


$name: Name of the term to search for.

$vocabulary: (optional) Vocabulary machine name to limit the search. Defaults to NULL.

Return value

An array of matching term objects.

5 calls to taxonomy_get_term_by_name()
TaxonomyHooksTestCase::testTaxonomyTermHooks in modules/taxonomy/taxonomy.test
Test that hooks are run correctly on creating, editing, viewing, and deleting a term.
TaxonomyTermTestCase::testNodeTermCreationAndDeletion in modules/taxonomy/taxonomy.test
Test term creation with a free-tagging vocabulary from the node form.
TaxonomyTermTestCase::testTaxonomyGetTermByName in modules/taxonomy/taxonomy.test
Test taxonomy_get_term_by_name().
TaxonomyTermTestCase::testTermInterface in modules/taxonomy/taxonomy.test
Save, edit and delete a term using the user interface.
TaxonomyTermTestCase::testTermMultipleParentsInterface in modules/taxonomy/taxonomy.test
Test saving a term with multiple parents through the UI.


modules/taxonomy/taxonomy.module, line 1228
Enables the organization of content into categories.


function taxonomy_get_term_by_name($name, $vocabulary = NULL) {
  $conditions = array(
    'name' => trim($name),
  if (isset($vocabulary)) {
    $vocabularies = taxonomy_vocabulary_get_names();
    if (isset($vocabularies[$vocabulary])) {
      $conditions['vid'] = $vocabularies[$vocabulary]->vid;
    else {

      // Return an empty array when filtering by a non-existing vocabulary.
      return array();
  return taxonomy_term_load_multiple(array(), $conditions);


ronan.orb’s picture

Unbelivable, that this function still has no parameter to filter the result by vid. Even though the they recommand to have the $options parameter deprecated for the inner function, you can use this for the vid filter.

function taxonomy_get_term_by_name_and_vocabulary($name, $vid) {
  return taxonomy_term_load_multiple(array(), array('name' => trim($name), 'vid' => $vid));

Should work.

girishmuraly’s picture

If you have the term name and want to find its term id in a particular vocabulary, this function could help. For e.g. since terms do not have machine names, they cannot be exported by features, unless their term ids match in the different dbs.

 * Helper function to dynamically get the tid from the term_name
 * @param $term_name Term name
 * @param $vocabulary_name Name of the vocabulary to search the term in
 * @return Term id of the found term or else FALSE
function _get_term_from_name($term_name, $vocabulary_name) {
  if ($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) {
    $tree = taxonomy_get_tree($vocabulary->vid);
    foreach ($tree as $term) {
      if ($term->name == $term_name) {
        return $term->tid;

  return FALSE;
andymantell’s picture

Instead of loading and looping over the entire taxonomy tree which could be potentially large, it would be better to use an EntityFieldQuery here. E.g. searching for the term "Foo" in vocabulary 1:

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'taxonomy_term')
  ->propertyCondition('name', 'Foo')
  ->propertyCondition('vid', 1)
jibran’s picture

Thanks It helps

output of this is

    [taxonomy_term] => Array
            [5] => stdClass Object
                    [tid] => 5
subhojit777’s picture

taxonomy_get_term_by_name does an case-insensitive search.

For example, term name is "MyTerm", then if you give taxonomy_get_term_by_name('myterm'), it will return taxonomy term object.

However, your query will not return any entity.

selva8187’s picture

Its working great.

RaulMuroc’s picture

Love it! Works like a charm :)

bluemaro’s picture

thanks foobar, that works!

jojonaloha’s picture

Just in case you try to use the second $vocabulary parameter in D7, it wasn't added until 7.14 http://drupal.org/node/1558424

cgmonroe’s picture

Another reason for the second parameter not working is that despite the implied case insensitivity in the function description, the vocabulary option is not. The vocabulary names are normalized to lower case for index keys. The underlying code uses this key to file the specified vocabulary but does not convert it to lower case.

decibel.places’s picture


crystaldawn’s picture

Put it all together is this is what it should be for D7 where this function simply does not work.

$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('name', 'Your Known Term Name Goes Here')
->propertyCondition('vid', 4) // 4 == Vid of the known vocabulary.

//Extract the Tid from the results. (Note the difference from the above comment, it was missing the return var which is required)
$YourNewShiny_tid = array_shift($result['taxonomy_term'])->tid;

lolandese’s picture

The function taxonomy_get_term_by_name comes in handy in submodules that create demo content. In that case on install terms are created for which the Drupal system assigned the tid's.

On uninstall you want those deleted but the tid's vary. Use something like:

 * Implements hook_uninstall().
function MY_MODULE_demo_uninstall() {
  // Remove the sample terms in taxonomy.
  $sample_terms = array(
    'Some term',
    'Another term',
    'A third one',
  foreach($sample_terms AS $item) {
    $tid = taxonomy_get_term_by_name($item);
    if (isset($tid)) {
        watchdog('MY_MODULE','Deleted demo term with tid ' . key($tid).': \'' . $item . '\'', $variables = NULL, WATCHDOG_NOTICE, $link = NULL);

Note that key($tid) is used and NOT $tid. The watchdog message can be omitted if desired.

lolandese’s picture

That should be:
function MY_MODULE_uninstall() {

diegosh’s picture

Hi , I needed to modify the value of a select list field with taxonomy term items, and I did the following snippet:

$taxonomy_id = taxonomy_get_term_by_name($node->type,'resource_type');
    // Get the tid of term linked with content type
    if(!empty($taxonomy_id)) {
      $tid = -1;
      foreach ($taxonomy_id as $taxonomy_item) {
        $tid = $taxonomy_item->tid;
      if ($tid!=-1) {
        $taxonomy = array('und' => 
        array(0 => array('tid' =>$tid)));
        $form_state['values']['field_res_resource_type'] = $taxonomy;
        // Construct the term taxonomy array ans assign it to this field.
mikedotexe’s picture

This came in handy when wanting to search all vocabs for a tid, and returning just the integer.
This assumes there is only one match in all the vocabs.

$termArr = taxonomy_get_term_by_name('Hospitals');
if (empty($termArr)) {
  drupal_set_message('No term found');
} else {
  $tid = array_pop($termArr)->tid;
  drupal_set_message('Term found: ' . $tid);

16am’s picture

This functionality appears to be deprecated in Drupal 8.
Use the taxonomy_term_load_multiple_by_name function instead.