function hook_views_query_alter
Alter the query before executing the query.
This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.
Parameters
object $view: The view object about to be processed.
object $query: An object describing the query.
See also
hook_views_query_substitutions()
Related topics
1 invocation of hook_views_query_alter()
- views_plugin_query_default::alter in plugins/
views_plugin_query_default.inc - Let modules modify the query just prior to finalizing it.
File
-
./
views.api.php, line 1167
Code
function hook_views_query_alter(&$view, &$query) {
// (Example assuming a view with an exposed filter on node title.)
// If the input for the title filter is a positive integer, filter against
// node ID instead of node title.
if ($view->name == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
// Traverse through the 'where' part of the query.
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
// If this is the part of the query filtering on title, change the
// condition to filter on node ID.
if ($condition['field'] == 'node.title') {
$condition = array(
'field' => 'node.nid',
'value' => $view->exposed_raw_input['title'],
'operator' => '=',
);
}
}
unset($condition);
}
unset($condition_group);
}
}