How to get term from alias
The only way to avoid a query is if the alias is already in the cache. Rather than worry about that yourself, it's best to use the API (even more so in Drupal 8 as path alias storage is pluggable and may not be in the database):


Drupal 7, using drupal_get_normal_path() :

$system_path = drupal_get_normal_path($path_alias, $langcode);

Drupal 8, using the path alias manager service:

$system_path = \Drupal::service('path.alias_manager')-&gt;getPathByAlias($path_alias, $langcode);


As always it's preferable to inject the service into the consuming class if context allows.


Both methods will get you the system path, e.g. taxonomy/term/1, from which you can extract the term ID.