I'm adding nodes to a view programmatically and i'm altering the pager 'x out of y' by setting the folowing in hook_views_pre_render:

$view->query->pager->total_items = $count;
$view->query->pager->current_page = isset($params['page']) ? $params['page'] : 0;
$view->query->pager->options['total_pages'] = ceil( $count / $lim );
$view->total_rows = (string) $count;
$view->items_per_page = (string) $view->query->pager->options['items_per_page'];
$view->current_page =isset($params['page']) ? $params['page'] : 0;

Let's say there are 10 pages but even with this code nothing seems to happen, nothing get's altered. When on the second page, it doesn't display the next button. How can this achieved with code? What object, array should i alter to have this result? Any help would be appreciated.



4 Answers 4


Try some thing like this and see if it helps

function views_pag_views_pre_build(&$view) {
    if ($view->name == "pagination") {


Thanks for all the great help. In hook_views_pre_render i used this 2 lines of code:

if($view->name=='myview'){ ...
   $view->query->pager->total_items = $count;       

And now the pager is working like a charm. :D The items per page value i have set it in the views ui, so i didn't have to change/set that programmatically.


For those using Drupal 8: If you're like me and made changes to your view in the 'hook_views_query_pre_render' hook then all you need to do to update the page is the following:

$view->result = $new_results; // Where $new_results is an array of new rows
$view->total_rows = count($new_results);
$view->pager->total_items = count($new_results);
$view->pager->updatePageInfo(); // After updating page variables then it's important to run this method!

Please note that the pager uses the variable 'total_items' and the view object uses the variable 'total_rows'...

Hope that helps someone!


I used the following code:

function yourmodule_views_pre_render(&$view) {
    $myview = views_get_view('view_id', TRUE);
    $myview->display['default']->display_options['pager']['options']['items_per_page'] = '5';

Your view id is the machine name of your view.

The snippet was borrowed from Programmatically Change Pager "Items to Display" value?

In order to clear the pager, the solution in the above thread also leaves the following snippet on the end.

$cache = $myview . ':block:results:';
cache_clear_all($cache, 'cache_views_data', TRUE);
  • 1
    I don't understand the line "views_save_view($myview);". Do you really want to save the view every time the hook is invoked? And empty the cache too?
    – ermannob
    Feb 28, 2014 at 11:37
  • In all honesty I'm not entirely sure. I was using this snippet from the thread listed, it worked and was relevant so I posted it here. However I ended up dealing with the pagers in a different way so I'm not actually sure. Here are some un-researched theories. If your changing the page pro grammatically you'd need to save it after the change. Isn't that what the views_save_view is doing? Isn't this hook only called once each time the view renders? Feb 28, 2014 at 15:10
  • No, that'll permanently save the View changes every time on pre_render. It shouldn't be necessary to save the View to make this work. May 5, 2014 at 20:51

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.