0

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!

2
  • Have you tried limiting the results for the view display? – Aiias Jun 28 '13 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 '13 at 21:39
0

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';
            }
            break;
}


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

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.

1
  • 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 '13 at 21:43

Your Answer

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

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