How to force Drupal views contextual filter to use logical OR condition

Drupal views
Drupal views

Drupal views by default uses the logical AND operator for contextual filters in ‘WHERE’ clause in views query. This is true when you have:

  • One contextual filter and one or more normal/exposed filters.
  • More than one contextual filters.

If you want to apply OR instead of AND, you will need to implement two hooks.

  • hook_views_query_alter() in file and
  • hook_views_api() in MY_MODULE.module

Note the index ‘0’ in code `$query->where[0][‘type’] = ‘OR’;`. This indicates the first group of conditions in ‘where‘ clause which is for contextual filters.

I am sure you know, why MY_MODULE is written in caps everywhere. If you don’t, put a comment here and we will answer of course. (But, will see how to get the treat from you first 😀 )

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.