hook_views_pre_execute() query not running (or being overwritten) I'm trying to completely overwrite the query that's created by Views UI by using hook_views_pre_execute() in a custom module. I know about the new database API and how this function expects a SelectQuery object, but I've also read (in a posting from Lullabot, so I think it's legit) that if you're just writing a single query and don't need to do anything conditional, it's perfectly OK to skip that and just use db_query() with raw SQL.

So here's my function - the drupal_set_message() line is executing, but the output of the view is still sorted in ascending order, which is what is set in the Views UI. My understanding was that this function was supposed to completely overwrite what's in the UI, so how do I get it to work? (Right now I haven't done anything funky with the SQL except change the order by just to see if it would work. There really is other stuff that needs to be done which justifies using a module instead of the Views UI.)

function group_speaker_carousel_views_pre_execute(&amp;$view){
    if ($view-&gt;name == 'speakers_carousel')  {
      drupal_set_message("view is {$view-&gt;name}");
       db_query("SELECT DISTINCT node.title AS node_title, node.nid AS nid, field_speaker_node.title AS field_speaker_node_title, field_speaker_node.nid AS field_speaker_node_nid,  'node' AS field_data_field_job_title_node_entity_type,  'node' AS field_data_field_company_node_entity_type
      }
FROM {node} node
LEFT JOIN {field_data_field_speaker} field_data_field_speaker ON node.nid = field_data_field_speaker.field_speaker_target_id
LEFT JOIN {node} field_speaker_node ON field_data_field_speaker.entity_id = field_speaker_node.nid
WHERE (((node.status =  '1') AND (node.type IN ('speaker'))))
ORDER BY field_speaker_node_title DESC");


    }
}
The reason nothing's changing is because you're just running db_query(), not actually assigning it to anything. If memory serves you need to assign your new query to $view-&gt;build_info['query'] (but that might be for Drupal 6). 

Also you won't unfortunately be able to use a vanilla db_query for this (as far as I know), because Views is expecting a db_select() object.

You can test this pretty easily by running the following two statements:

// Breaks the view.
$view-&gt;build_info['query'] = db_query('SELECT nid FROM {node}');

// Does not break the query (but results are wrong obviously).
$view-&gt;build_info['query'] = db_select('node', '')-&gt;fields('n', array('nid'));


I would look at re-working your query into a dynamic db_select() and then assigning it as in the above example, you should get more joy.