Need some help with your project? Contact me

Using complex and/or filtering with Views in Drupal 7

Recently I was faced with a need I have to admit I never faced before. I had to create a View that displayed content of two different types but one of which published only in a given timespan whereas the other one published whenever.

Initially I kept thinking that with Views this will be very difficult because the filters that you apply affect all the content that is being displayed in it. So if you need to filter by a field present in both content types but only need to restrict something on one of them, it’s perhaps not the most obvious to find the solution. It is though very easy as it turns out - thanks to my colleague who pointed out this trick to me.

So let’s assume you need to have Articles and Basic Pages shown in the View. And let’s say that all the Basic Pages need to show up (regardless of their publishing date) but only Articles published in the previous day can show up. So what do you do?

First of all, add a filter that checks whether the content is published and another one that checks if it is of the type Basic Page. Next, add yet another filter that checks whether the content is published, one that checks that the date of publishing equals - 1 day (or whatever your criterion is) and another one that checks if it is of the type Article. So in total you have 5 filters.

On their own, these filters seem confusing as they conflict. But there is one extra step you need to do: wrap them in an and/or logic. Instead of clicking to add a new filter, click on the little arrow that brings down 2 more options: and/or and rearrange. Choose the first one and you can set up the logic kind of like in this image below.

This will display only the nodes that match these criteria, namely what we set out to do.

Hope this helps!


Nice one!

Wish we could nest and/or, it would make this setup less repetitive: is published and of type Article, Page and (of type Page or Post date = -1 day).

I have written the condition:
Content publish = yes And Content type = 'Video'
Content publish = yes And Tags (field_tags) = 'Video'

return only one node.
When I write:
Content publish = yes And Content type = 'Video'

return all nodes but obviously not only node with tags = 'Video'.
Any idea?

Hi, I was wondering if and how this and/or wrapping can be applied to contextual filter combinations, to be more specific "not in URL contextual filter" ?

For example :
I have 3 types of products to show in a catalog:
1. Only visible for logged in user (owner of the product)
2. Visible for all logged in users
3. Visible for all logged in users + anonymous users

The filter would then be something like this :
_ content type = product
_( (product_visibility = user AND product_owner = logged_in_uid ) OR ( product_visibility = all_users AND user is logged in) OR product_visibility = anyone)
_content = published

In fact I only need 1 variable which is uid in case product visibility is limited on product owner.
That fact forced me to switch to contextual filter for this. Or can it be done otherwise ?

I'd like to do the same sort of thing except one of my filters is an exposed fulltext search filter. I need to repeat the exposed filter in both groups. But then the search input shows up twice on my search page.

This filter looks does not work because there are 2 searches:
(search AND published AND nodeType1) OR (search AND published AND nodeType2 AND flag)

It needs nested and/or, which does not seem to be available:
(search AND published AND (nodeType1 OR (nodeType2 AND flag)))

@CptCasual did you find a solution for this? I'm running into the same issue...

I was able to move the logic around so "search" was only needed once. Since "flag" only applied with nodeType2 I was able to drop nodeType2 from the filter. In fact it was even possible to add a location filter on top of the search filter. So it turned out simply like this...

(flag OR nodeType1) AND (search AND published AND location)

Will this work with relationships and when the filters are using relationships. I have tried this but can't seem to get it working?

I face this problem quite long before but that time I was unaware of this, so I wrote logic in views template files accordingly.
But I still remember how complex and tricky it was.
But here the solution is perfect & simple.

Have faced such a need a lot many times while working with views but I had no idea of existence of any such functionality. Every time I used to find some workaround for my needs. After so many months working with drupal I got this solution here. I strongly recommend this. Great article.

Thanks for sharing this, this lead me on the right path to setting up an OR group of filters.

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
$var = 'my variable';