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

Add new comment