Get taxonomy term ID from standard URL and then pass it to a View as a contextual filter
You do not have to write any code to achieve that.

Just go to the views edit page, add a contextual filter named as Has taxonomy term ID and select Provide default value, see screenshot for more options



However, you can also do it pragmatically

function HOOK_views_query_alter($view, $query) {
   $term = menu_get_object('taxonomy_term', 2);
   if ($term &amp;&amp; $term-&gt;vocabulary_machine_name == 'VOCABULARY_NAME') {
     if ($view-&gt;name=='VIEWS_MACHINE_NAME') {
       $view-&gt;query-&gt;where['0']['conditions']['0']['value']= $term-&gt;tid;
     }
   }
}


I hope it helps.