I couldn't find a similar problem on the forum so I am asking. I created a view with exposed filters but when I visit the view page I am going display 5 top recent record for specific content type. When I click search button the result changes of course and result with a pager shows up. How can I display 5 top recent records before I click search button for the first time when I visit page with the view? I thought to use hook_views_query_alter to add limit to the query but I don't know how. Any ideas how to achieve that?

Thank you!

  • Have you tried limiting the results for the view display?
    – Aiias
    Jun 28, 2013 at 21:14
  • Yes, I tried but it doesn't work for me because before you filter the view with exposed filters all records (with 5 on each page) are displayed with a pagination. I want display only top 5 by default without pagination.By the way I use Infinite Scroll instead of a default pager.
    – user7929
    Jun 28, 2013 at 21:39

2 Answers 2


I will show you a sample of how to do it...

In this case if you want to alter the nid to be passed as query to views via filter

function hook_views_query_alter(&$view, &$query){
    switch($view -> name){
        case 'view_name':
            if($view->current_display == 'block'){
                $nids = calculate_nid();
                $nids = !empty($nids) ? $nids : array(0);
                $query->where[1]['conditions'][2]['value'] = $nids;
                $query->where[1]['conditions'][2]['operator'] = 'IN';

function calculate_nid() {
// calculations to get nid. 
// here you can write sql queries

If within the Views Pager options, you select "Display a specified number of items" and set "Items to display" to 5, you will get the behavior you are desiring. You just need to make sure your sort criteria provides whatever "top" means on your site. What I think you are doing instead is selecting "Use Pager" with 5 items.

  • Hi Jimajamma, thank you for your answer but it is not what I want. "Display a specified number of items" with amount of 5 works for each result so when you try find something with exposed filter and query return more than 5 records (for example 10) then only 5 records will be displayed without pagination. Do you know how to modify query adding limit to it with hook_views_query_alter? Thank you!
    – user7929
    Jun 29, 2013 at 21:43

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.