I have a view using taxonomy CONTEXTUAL FILTERS.

  1. taxonomy vocab 1 terms AND
  2. taxonomy vocab 2 terms

Both are set to get the taxonomy id from the url, limited to a specific taxonomy vocabulary and share any of the terms.

But on taxonomy vocab 2, I would like to have an additional filter to pull content without any vocab 2 terms at all.

something along the lines of

  1. has taxonomy vocab 1 terms AND
  2. has taxonomy vocab 2 terms OR
  3. has NO taxonomy vocab 2 terms

I can do this with two separate views:


  1. has taxonomy vocab 1 terms AND
  2. has taxonomy vocab 2 terms


  1. has taxonomy vocab 1 terms AND
  2. has NO taxonomy vocab 2 terms

but I would like to combine the results.

Edit: if I have this set of content with terms, and the Contextual argument is term 1 I want the first and third to be shown in Views results:

content 1 | term 1
content 2 | term 2
content 3 | no term selected

  • I may be missing something here but you seem to be contradicting yourself. If you have the filter to both include and exclude by some criteria then you don't need that filter at all. Can you add examples of your desired results output to clarify what you want?
    – prkos
    Mar 26, 2019 at 14:33
  • Hi, the vocab 2 has several terms, but they are not required. So can have content as follows: content 1: vocab 2: term 1; content 2: vocab 2: term 2; content 3: vocab 2: no term selected: If the contextual filter has 'term 1', I want to pick up both content 1 and content 3. Content without a vocab 2 term should apply to all.
    – ice70
    Mar 27, 2019 at 8:06

3 Answers 3


You can draw some clues from this question: Views contextual filters for NULL value You should use Computed field or some other way of introducing custom PHP into your Views (custom module is the best option).

If you use more than one Contextual and need to have the OR operator between them you can use the Views Contextual Filters OR module.

There is also a patch for the D7 version you can expand on: Add an "OR IS NULL" option to contextual filters.

But since you're using two vocabularies and need to maintain the AND between the two contextuals you won't be able to use the "OR" module and you'll have to come up with a custom code solution for the second contextual that takes care of the both "there is specific term" and "there are no terms" in one contextual filter, so you can combine it with the contextual for the Vocabulary 1.


I needed to convert a contextual filter to be an IS NULL condition. I was able to do this with a query alter. I set my contextual filter to a fixed value of string "null" and then created a custom module with a MYMODULE.views_execution.inc file.

// loop over where groups
  if (is_array($query->where)) {
    foreach ($query->where as $whereIndex => $where) {

      // loop over where group conditions
      if (is_array($where['conditions'])) {
        foreach ($where['conditions'] as $conditionIndex => $condition) {

          // loop over group condition values
          if (is_array($condition['value'])) {
            foreach ($condition['value'] as $valueIndex => $value) {

              // if the value is the string of 'null'
              if ($value === 'null') {

                // convert the condition to a 'IS NULL' query
                $query->where[$whereIndex]['conditions'][$conditionIndex]['field'] = strstr($condition['field'], ' =', true);
                $query->where[$whereIndex]['conditions'][$conditionIndex]['value'] = NULL;
                $query->where[$whereIndex]['conditions'][$conditionIndex]['operator'] = 'IS NULL';

Source: https://gist.github.com/devkinetic/02439eff676d0ad89a558e0b2693b9e8


This appears to be an issue in Drupal Core. You can apply a patch from there that is appropriate to your version of Drupal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.