How to Alter Views query in Drupal 7

, , ,



Altering a query is to change the results as desired by the user. hook__views_query_alter() in Drupal is for altering the query. Before start reading, you should have an idea about what is a view and how views created? See here.

Understanding hook_views_query_alter().

This hook function, the query for the views content result could be altered before it execute, we can get the query value in $view->query.

The sample code for hook_views_query_alter() :

function mymodule_views_query_alter(&$view, &$query) {
 if ($view->name == 'myview') {
  dpm($view->query); //Displays the query for the view named 'myview'.
  dpm($view->query->where); //Displays the where condition part values in the query.
  dpm($view->query->orderby); //Displays the orderby part values in the query.
 }
}




Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.