I'm very surprised I can't find an answer anywhere, since this seems to me to be a simple, basic question. How can I get the term id (tid) for a taxonomy term from the term name? That is, for the term itself. I would like to do this in the page.tpl.php file.

I would like to be able to take a taxonomy term such as: MO-10-004

And get the tid, for example: tid = 159

The vocabulary id in this case (vid) is 4.

I guess my real question is, "Is this information already available to me within the page.tpl.php file in an existing variable, which I can access?" And if it is, how do I access it? I'm assuming the taxonomy module has already loaded the term data from the database and put it in a variable somewhere, so I don't want to add another SQL query if it's already been done.

Comments

nevets’s picture

Where are you getting the taxonomy term from? The path, a node, something else?

joep.hendrix’s picture

taxonomy_get_tid_by_name()

-----------------------------------------
Joep
CompuBase, Dutch Drupal full service agency

DizzyC’s picture

marcoka’s picture

attention: This can result in multiple returns (array). So if you want to get the tid for "foobar" it could result in an array of tids. In most cases it is not possible to get a unique "termanme id".

dizarter’s picture

taxonomy_get_term_by_name() will return array of terms if there are multiple terms with same names.

You should then iterate through that array and do a conditional check based on other term properties to find a unique one (check for parent ID, vocab ID etc.) However, this doesn't ensure unique match either, as only unique term field is TID.

drupalshrek’s picture

An example where the taxonomy names are known to be unique:

    $terms = taxonomy_get_term_by_name("All");
    $all_term = current($terms);
    $terms = taxonomy_get_term_by_name("Words");
    $words_term = current($terms);
    $all_tid = $all_term->tid;
    $words_tid = $words_term->tid;

drupalshrek

picacsso’s picture

// Get's the tid from term name
$tid = key(taxonomy_get_term_by_name('some term name'));

imyaro’s picture

key() function expects to have &$array as param so we can't use it with value that function returns.
Better will be use construction like this
$terms = taxonomy_get_term_by_name('some term name');
$tid = key($terms);
or
$tid = reset($terms)->tid;

P.S. Don't forget to check if function result is not empty

Kipp Grose’s picture

This worked, cheers.   (I only have one instance of the term I'm after, if that's your situation, this will work for you too).

$terms = taxonomy_get_term_by_name('some term name');
$tid = key($terms);

baltazarz3’s picture

This worked for me. Thanks man

ojchris’s picture

Works thanks. Without the key you get a bunch of nested array

Boldizart’s picture

pratishjha’s picture

For drupal 9 get taxanomy id by term name
 

$termId= \Drupal::entityQuery("taxonomy_term")->condition("vid", "vocab_name")->condition("name", "term_name")->execute();

you can give your vocabulary name and term name.

Thanks