how do I add a table and where clause to query object using hook_views_query_alter()? I currently have a unique situation whereby I need a to use a different term reference field in a view's contextual filters based on a node's content type value. I don't want to have to manually add a display for each possible combination of content types so I have been tying to use hook_views_query_alter() to accomplish what is now seeming impossible to me.

In my .module file I have:

/**
 * Implementation of hook_views_api().
 */
function mymodule_views_api() { // your module name into hook_views_api
  return array(
    'api' =&gt; 3,     
    'path' =&gt; drupal_get_path('module', 'mymodule').'/includes',
  );
}


Then in the file mymodule/includes/mymodule.views.inc:

/**
 * Implements hook_views_query_alter()
 */
function mymodule_views_query_alter(&amp;$view, &amp;$query) {
   //this is the name of our View and Display
   if ($view-&gt;name == 'shop' &amp;&amp; $view-&gt;current_display == 'listings') {

       $query-&gt;add_table('field_data_field_my_category');
       //then add where clause afterwards etc etc
   }
}


This throws the error:


  Fatal error: Call to undefined method SelectQuery::add_table()


Which leads me to believe that the Views API is not loading properly, unless I'm completely on the wrong track here. 

I've been trying to find the best practices for altering views queries but have just ended up being generally confused and frustrated. Could anyone lead me towards the light please?
I don't know enough about your problem to say whether using  hook_views_query_alter is the best approach.

In regards to the error you're getting, the answer is in the error message: the query object you have is an instance of SelectQuery. If you look at the documentation for SelectQuery you will see that it doesn't have an add_table method.

Instead you should use SelectQuery::addJoin (or one of the more specific methods, such as SelectQuery::innerJoin, SelectQuery::leftJoin, etc.)