how to use hook_views_query_alter() to modify where condition?

Solutions

You can access the content of a where condition modifying its value:

$query->where[0]['conditions'][0]['value'] = 'something...';

Pretty similar what you did with orderby. Also you can add custom where conditions (https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where/7 and https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/function/views_plugin_query_default%3A%3Aadd_where_expression/7)

To add a new where clause you can use, add_where

For example :

$query->add_where(1,'taxonomy_term_data_node.tid', $value, 'NOT IN');
function mymodule_views_query_alter(&$view, &$query) {
  // Term id's to unset from result set.
  $tids = array(346,355,359);
  if ($view->name == 'yourviewname') {

    $query->add_where(1,'taxonomy_term_data_node.tid', $tids, 'NOT IN');
    $query->orderby[0]['field'] = "CASE WHEN taxonomy_term_data_taxonomy_term_hierarchy.weight IS NULL THEN taxonomy_term_data_node.weight ELSE taxonomy_term_data_taxonomy_term_hierarchy.weight END";
    $query->orderby[0]['direction'] = "ASC";
    $query->orderby[1]['field'] = "taxonomy_term_data_node.weight";
    $query->orderby[1]['direction'] = "ASC";
  }
}
Tags: Drupal 7 / Views

Similar questions

How to hook_views_query_alter arbitrary field and use it in view GUI
For the life of me I can't figure this out. I have vast and complicated drupal 7 site and among other things I'm using profile2 module. Not sure why, but a few years back I've added field directly to 'profile' table. It is used to store some number and adding it looked like quick fix back then. Now I need to get some info using views and all is fin...
how to find out if a custom filter handler is in use when in hook_views_query_alter()
Drupal 7. I have a custom filter handler which needs to amend the views query. As the filter can be added to any view I can't simply check for view name in the hook_views_query_alter() I know the filtername exists somewhere in the $views parameter to hook_views_query_alter() within its arrays of array of objects I have yet to find an efficient way ...
How do I use hook_views_query_alter() to re-write a query?
I have a very simple view that lists all the nodes, which shows the following query: I am trying to overwrite it with a custom database query to list only nodes of the user referenced in the URL and all his invitees. This is the code I am using. dpm() shows all the correct nodes, but reading hook_views_query_alter() I still can't figure out how to ...
How can I use hook_views_query_alter()?
How can I implement hook_views_query_alter() in Drupal 8? In Drupal 7, I could use the following code. In Drupal 8, this code doesn't work. Can anyone help me out?
How to set the filter condition by default for specific condition in view
How to set the filter condition by default for specific condition in view i,e i have view with the filter condition STATUS drop down list has 1]live 2] discard,so here i want to filter by default for live status when i open this view can any please tell me how to do this? Thanks a lot in advance.
Drupal 7 OR condition with a series of AND condition
I have a `Drupal query like below I would like to add an OR condition to this query. So it will be a global OR condition. I added a code like below But it didn't add an OR condition but put the condition query within braces in an AND condition. How can I achieve an output query like by using Drupal query builder? Please help

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.