Using hook_query_alter I am using hook_query_alter to make changes in get taxonomy terms query.

    function MYMODULE_query_alter(QueryAlterableInterface $query) { 
    global $user;
    if($user-&gt;uid &gt; 1) {
        if ($query-&gt;hasTag('term_access') &amp;&amp; strstr(current_path(), 'admin/structure/taxonomy')) {
            $permission = $query-&gt;leftJoin('term_permissions_user', 'tpu', "t.tid = tpu.tid");      
            $ored = db_or();        
            $ored-&gt;condition('uid', NULL, 'IS');        
            $ored-&gt;condition('uid', $user-&gt;uid, '=');
            $anded = db_and();
            $anded-&gt;condition($ored);
            $query-&gt;condition($anded);
        }
    }
}


I want to have above query clause on the page which lists taxonomy terms only but this hook is adding the clause on all pages. I mean this hook is called on any page load of my drupal site. 

Other issue is that the alias for table taxonomy_term_data is base on same pages and t on other pages and if i join using alias t it gives me error on most of the pages and vice versa.

I am checking the table alias to ensure the use of table alias used in my query and have changed the code as 

function MYMODULE_query_alter(QueryAlterableInterface $query) { 
    global $user;
    if($user-&gt;uid &gt; 1) {
        if ($query-&gt;hasTag('term_access') &amp;&amp; strstr(current_path(), 'admin/structure/taxonomy')) {          
            $tables = $query-&gt;getTables();
            if(isset($tables['t']) &amp;&amp; isset($tables['t']['alias']) &amp;&amp; $tables['t']['alias'] == 't' &amp;&amp; $tables['t']['table'] == 'taxonomy_term_data') {
                $permission = $query-&gt;leftJoin('term_permissions_user', 'tpu', "t.tid = tpu.tid");      
                $ored = db_or();        
                $ored-&gt;condition('uid', NULL, 'IS');        
                $ored-&gt;condition('uid', $user-&gt;uid, '=');
                $anded = db_and();
                $anded-&gt;condition($ored);
                $query-&gt;condition($anded);          
            }
        }
    }
}


But I am not sure if I have used a correct check, as of now my aim of adding query clause is fulfilled. Just to ensure this will work for long and will not break in any case.
Please help me to have this hook call on the page where I need this and use the exact table alias.

Thanks in advance for your help.
hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id =&gt; $info) {
    // If 'join type' is not set, then it's the main table.a
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}


I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
  global $user;
  if ($user-&gt;uid &gt; 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mymodule_get_main_table_alias($query-&gt;getTables())) {
        $alias = $query-&gt;leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query-&gt;condition(db_or()
          -&gt;isNullcondition("$alias.uid");        
          -&gt;condition("$alias.uid", $user-&gt;uid, '=')
        );          
      }
    }
  }
}


Notice that:


In $query-&gt;leftJoin(), %alias is replaced with the alias the query is using for the table  being joined. As you are altering an existing query, you cannot know if the alias you chose is already used.
The result of $query-&gt;leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query-&gt;condition() are ANDed with each other.