views_query_alter never called I want to alter a query for sorting a field. I read the documentation, it says


  This hook should be placed in MODULENAME.views.inc and it will be
  auto-loaded. MODULENAME.views.inc must be in the directory specified
  by the 'path' key returned by MODULENAME_views_api(), or the same
  directory as the .module file, if 'path' is unspecified.


I used to implement all hooks inside my sites/default/realia/template.php so I first went stright and implemented the hook in sites/default/realaia/template.php which obviously didnt work.

function realia_views_query_alter(&amp;$view, &amp;$query) {
    print "&lt;h1&gt;I am in alter query&lt;/h1&gt;";
    var_dump($query);
}


After reading doc carefully which says the hook needs to be placed inside the specific file and in a specific place I totally feel lost. Because I don't see any .module file in my sites/default/themes/realia. This is where my all files provided by realia (3rd party) are placed there is only .info file but no .module file.

There are also other related modules inside sites/all/modules/realia but looks like this folder is not a module itself becuse there are no .info or .module files inside the root but subfolders/modules has it. I went and created a file reala_submodule.views.inc with the implementation and placed one in each of the modules but I still dont see it really got executed ever.

Of course I did drush cc all every time whenever I doubted there could be caching issue. I will have followup questions related sorting once I get this implementation working. Please help.
Maybe  reason can be if you use Search API module, in this case hook_search_api_views_query_alter will work instead

link 1
link 2

I have the same problem now and after some search find that when search API module enabled  hook_views_query_alter not worked.