In order to modify the where condition of a views query we use hook_views_query_alter. This hook should be placed in your custom module.
//Here, we are going to modify the query, which is an element of the $query variable
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
if ($view->name == 'my_view' ) {
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
if ($condition['field'] == 'node.title') {
$condition = array(
'field' => 'node.nid',
'value' => $view->exposed_raw_input['title'], // can change value
'operator' => '=', // can change operature here if required
);
}
}
}
}
}
0 Comment(s)