OR operator in Drupal View Filters
drupal, drupal-views
Solution
if you want do it with view_query_alter hook, you should use $query->add_where() where you can specify if it's AND or OR. From views/include/query.inc
/**
* Add a simple WHERE clause to the query. The caller is responsible for
* ensuring that all fields are fully qualified (TABLE.FIELD) and that
* the table already exists in the query.
*
* @param $group
* The WHERE group to add these to; groups are used to create AND/OR
* sections. Groups cannot be nested. Use 0 as the default group.
* If the group does not yet exist it will be created as an AND group.
* @param $clause
* The actual clause to add. When adding a where clause it is important
* that all tables are addressed by the alias provided by add_table or
* ensure_table and that all fields are addressed by their alias wehn
* possible. Please use %d and %s for arguments.
* @param ...
* A number of arguments as used in db_query(). May be many args or one
* array full of args.
*/
function add_where($group, $clause)
Problem
I need to implement an OR operator between some filters in a Drupal View. By default, Drupal AND's every filter together. By using ``` hook_views_query_alter(&$view, &$query) ``` I can access the query ( var $query ) , and I can change either : ``` $query->where[0]['type'] ``` to 'OR', or ``` $query->group_operator ``` to 'OR' The problem is however, that I do not need OR's everywhere. I've tried changing both of them to OR seperately, and it doesn't yield the desired result. It seems changing those values, puts OR's everywhere, while I need => ( filter 1 AND filter 2 ) OR ( filter 3 ), so just 1 OR. I could just check the Query of the View, copy it, modify it, and run it through db_query, but that's just dirty .. Any suggestions ? Thx in advance.