Profile picture for user admin
Daniel Sipos
07 Nov 2013

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!

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Capi Etheriel 11 Nov 2013 18:04

Nesting and/or

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

Sergio 22 Jul 2014 00:54

2 And - Or - 2 And

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

rawdesk 04 Sep 2014 20:10

contextual filter usability

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 ?

CptCasual 07 Feb 2015 08:33

same problem but with exposed search filter

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 26 May 2015 20:01

In reply to by dan_rogers (not verified)

fixed with logic

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)

Martin 27 Jul 2015 16:33

grouped filters with relationships

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

Rakesh Nimje 25 Nov 2015 07:12

Very well explained the complex scenarios with easy solution

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.

Sonam 29 Jan 2016 07:39

Very nicely explained.

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.

Andrae 04 Jun 2017 14:09

This helps a lot!

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

Add new comment