Drupal: Convert taxonomy term name to term id

imgrgry picture imgrgry · Apr 30, 2010 · Viewed 11.4k times · Source

Using 'taxonomy: term ID' as an argument in one of my Drupal views and I'm having trouble getting it to work correctly.

On the views administration page it states, "if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name."

How does one go about converting the term name from a URL into a term ID?

Answer

Nikit picture Nikit · Apr 30, 2010

you didn't show how do link to this views with agruments, if like this: http://site.com/views_page/taxonomy_name, then: arg(1) - it's you term_name, get id via: $term = taxonomy_get_term_by_name(arg(1));
result is $term->tid

But if http://site.com/views_page/taxonomy_name - is aliased path to taxonomy terms, views should see directly tids in argument handling via arg(2) in taxonomy/term/TID path.