I've used display specified number of items for set limit in views. I want to change limit and also display all items using hook_views_query_alter(). I've tried this:

function test_views_query_alter(&$view, &$query) {
  if ($view->name == 'sample') {
    //~ $view->set_items_per_page(20);

But i've no luck. How to solve this?

2 Answers 2


Try using the following:

function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) { 
  • I got this error "Fatal error: Call to undefined method view::set_use_pager() "
    – Guru
    Commented Jan 7, 2016 at 6:09
  • Please remove set_use_page line. Pager should work with set_items_per_page only.
    – Kalpan
    Commented Jan 7, 2016 at 6:36
  • It is working fine for alter limit only. If i want to remove pager, then how can do that?
    – Guru
    Commented Jan 7, 2016 at 7:18
  • I get the same error. I guess this answer if for Drupal 7 though. For Drupal 8, using $view->getPager()->setItemsPerPage($nb); as said in another answer works fine.
    – Progzy
    Commented Sep 14, 2018 at 9:47

Drupal 8.

I created views in rest mode for send 10 items per page. But the native pager was not works for me. So, I implemented hook_views_query_alter() for set up custom pager settings with limit and offset.

1) Create custom_rest_api.views_executable.inc file for implement the hook

2) Set views offset and page limit

 * @file
 * Containts custom_rest_api.views_executable.inc.

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

 * Implements hook_views_query_alter().
function custom_rest_api_views_query_alter(ViewExecutable $view,     QueryPluginBase $query) {
   if ($view->id() == "news" && !empty($view->args)) {
   $number = (int) ($view->args[0]);

   $limit = 10;
   $offset = $limit * $number;

  • Thanks! Took me some time to remember $view->getPager()->setItemsPerPage($limit); is only working when pager option is set to 'Display a specified number of items' Commented Apr 20, 2020 at 22:03

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.