I want to alter the view query before rendering the view, so I use the hook_views_pre_execute. I want to add an 'WHERE' 'AND' condition clause to the query. So here is my code :

MYMODULE_views_pre_execute(&$view) {
    // some code here to get my node id to exclude
    $view->query->add_where('1', 'node.nid', $nid_exclude, '!=');

but it seems to me the hook not working, or I did mistake to the add_wherefunction?

UPDATE : The hook is executed but the view is not modified.

  • 2
    Does "not working" mean that the hook never executes, or that the View does not modify it's behaviour?
    – Letharion
    Dec 10, 2014 at 14:48

1 Answer 1


To alter query you should use hook_views_query_alter

Sample Code using hook_views_query_alter :

function MODULE_views_query_alter(&$view, &$query){
switch($view -> name){
    case 'products':
        if($view->current_display == 'block_1'){
            $nids = get_nid();
            $nids = !empty($nids) ? $nids : array(0);
            $query->where[1]['conditions'][2]['value'] = $nids;
            $query->where[1]['conditions'][2]['operator'] = 'IN';

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.