I've followed the Drupal 7 documentation for hook_query_alter() in order to limit the number of total search results when searching for nodes or users to an arbitrary number.

However, with the following code (and the example in the documentation) this function has no effect:

function mymodule_query_alter(QueryAlterableInterface &$query)
    $query->range(0, 3);

The code is getting executed, because adding a line using the dpm() function works and information is outputted, so the hook is getting executed. However, the $query->range(0, 3) method appears to have no effect.

What am I doing wrong?


I am using the custom_search module for Drupal 7.

I can change the number of results displayed on each page with the following code in hook_query_alter():

$results_per_page = 20;

if ($query instanceof PagerDefault
    && ($tables = $query->getTables())
    && isset($tables['i']['table'])
    && $tables['i']['table'] === 'search_index')

But what I'd like to do is limit the total number of results before being paginated; if there are 234 results, I'd like to limit the number to, say, 100.

I am guessing that this is not working because I'm using the custom_search module.


  • 1
    Is the query tagged? drupal.org/node/310077 – Cyclonecode Jun 19 '15 at 15:22
  • @Cyclone if hook fires, then query was tagged. But we don't know if the query OP wants is. We don't know if this code fires for his query. I strongly suggest to only use (api.drupal.org/api/drupal/modules%21system%21system.api.php/… – Mołot Jun 19 '15 at 16:01
  • @Mołot - I'm not so sure that the hook fires, that the reason I'm asking. – Cyclonecode Jun 19 '15 at 16:29
  • @Cyclone OP wrote he used dpm in hook, and it fired. But he didn't write what it shown, so we don't know for what it fired... That was my point. By the way, unconditional limiting queries is... Bold move. I wouldn't dare. – Mołot Jun 19 '15 at 16:33
  • I apologize for any confusion. The hook is executed, but the $query->range method doesn't change anything. – Paul Kerrigan Jun 22 '15 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.