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!

Comments

Hi,
I have written the condition:
Content publish = yes And Content type = 'Video'
or
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?
Thanks

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)))

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)

Add new comment