Limit records displayed in a view using hook_views_query_alter on a Drupal 6 website

Submitted by ionut on 13/05/2011


drupal, views, query, hook

Probably at some time you will need to modify some view's output by code...

So, here's how you can for example limit the output to a certain number of records...

1. Implement a hook_views_query_alter in your custom module

function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {

2. As you probably realized, there are 2 objects available that can be altered: $view and $query.

The view class is defined in [path_to_modules_folder]/modules/views/includes/
and the views_query class is defined in [path_to_modules_folder]/modules/views/includes/ - Open them and see what properties and methods are available.

Let's limit the output to 2 records with


and also let's hide the pager



Complete example:

function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {

That's it!

Happy coding

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.