Hello.
I would like to add a filter to a view with two fields, but with "or" condition between them.
What I need is: FIELD1 is Not NULL or FIELD2 is Not Null.
I don't know how could I do it. Always filters fields use "AND" between them.
Thanks.

Comments

DiversiCon’s picture

Including an "OR" condition is not an option via the standard Views UI and, as far as I know, there are no contributed modules which add that functionality. However, you can create a custom module that will do exactly what you want. By using the hook_views_query_alter function in your module you can change any part of the query, including the where clause:

This is for Drupal 6/Views 2

/**
 * Implementation of hook_views_query_alter().
 */
function MODULENAME_views_query_alter(&$view, &$query) {
  if ($view->name == 'your_view_name') {
    // Adds a new where group to the query
    $view->query->add_where('where_group_id',"(table1.field1 = '%s' OR table2.field2 = %d)",'mall',1);
    $view->query->set_group_operator('AND');
  }
}

If you examine the contents of the $view object within the above hook (via debug, var_dump or print_r) you will be able to review the query for clearer picture of what is happening. The only trick after this is remembering that the code is there and why (documentation IS important).

If you're new to custom modules you can take a look at these pages for more information:
http://drupal.org/node/416986 - Module in three easy steps.
http://drupal.org/node/231276 - Creating Drupal 6 modules.
http://drupal.org/node/42609 - Views Module Developer API.
http://drupalcontrib.org/api/function/hook_views_query_alter/6 - API info for views_query_alter.

Good Luck!!!

WorldFallz’s picture

Probably the simplest way is to upgrade to views v3 but there is also the http://drupal.org/project/views_or module.

DiversiCon’s picture

Excellent! I hate that I have missed finding Views Or until now...been working way to hard to solve a simple issue. Thanks a million!

WorldFallz’s picture

yeah, it's easy enough to miss the exact module you need when there's like 5k of them to search through, lol.

briantes’s picture

Thanks very much. Views or do what I need.

rhizomenetworks’s picture


function cg_relation_views_query_alter(&$view, &$query) {
    if ($view->name == 'user_relations') {
        global $user;
        $view->query->add_where('login_user', "field_data_field_relation_user1.field_relation_user1_target_id", $user->uid, "=");
        $view->query->add_where('login_user', "field_data_field_relation_user2.field_relation_user2_target_id", $user->uid, "=");
        $view->query->set_where_group('OR', 'login_user');
    }
}

The example below refer to entity ("Relation") that has two Field Reference: field_relation_user1 and field_relation_user2 referring to user entity. The query intend to bring Relation instances when uid of current login user match one of the fields. Accordingly performing OR on two fields,

In order for this query to work you need to add "RELATIONSHIPS" for the two fields. Without adding the relationship the referred table.column:
ield_data_field_relation_user1.field_relation_user1_target_id
ield_data_field_relation_user2.field_relation_user2_target_id

Are not recognized.

How it work:
The first line of the example add new group to "where" object named 'login_user' and set the first "condition" for this group for the first field. The second list will set another condition for the second field on where group "login_user". Finally the last line of code change the operator to apply on the two conditions set for group "login_user" to OR (by default AND is created).

Hope this help.