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(&$view, &$query) {
    print "<h1>I am in alter query</h1>";

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.

  • You need to write your own module for this, is that the bit of info you're missing?
    – Clive
    Commented Feb 27, 2014 at 10:09
  • I created a new module with .info, .module and mymodule.views.inc with the hook implementation. I dont see its called ever (the debug statements are never rendered on browser)
    – Dudepal
    Commented Feb 27, 2014 at 10:37
  • Please do not change meaning of a question once it got answers...
    – Mołot
    Commented Feb 27, 2014 at 11:05

2 Answers 2


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.

  • Thanks a billion, I had been banging my head figuring out why hook views query alter is not invoking :) Commented Sep 22, 2019 at 16:33
  • @InspiredCoder you're very welcome. thanks for your comment. I feel good when I saw my answer help another guy ^__^
    – Yuseferi
    Commented Sep 22, 2019 at 21:10

It's not called because it's too late to call it from theme layer. You should call it from a module.

  • please check my updated question. I implemented query alter in new module. But wondering why its not called
    – Dudepal
    Commented Feb 27, 2014 at 10:59
  • 1
    Put it in the module body, rather than in another file. See if it works there. Commented Feb 27, 2014 at 14:11
  • That did the trick. Can you please help me understand why it didnt worked in the way docs said? I have a separate post [here] (drupal.stackexchange.com/questions/104985/…)
    – Dudepal
    Commented Feb 27, 2014 at 15:17
  • When you add an inc file, you should include it in a module or in an inf file. Sometimes you don't. But in this case, obviosly, the file was not getting called. Commented Feb 27, 2014 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.