18

After browsing the net and SE for a while it appears to me there is no good solution to combine 2 contextual filters with OR (rather than the default AND).

This post mentions a PHP hack and this post has some links to an issue stating a recent change in views 7.x-3.1. It also mentiones using hook_views_query_alter(&$view, &$query) to modify the view.

Does anyone have a working example or a better solution?

  • I don't think there is a better solution as of now. As said in the threads you pointed, it's an ongoing issue apparently. I stumbled on the same problem this week, and had to find a workaround with attachments. But then, it doesn't give a mixed list but two connected ones, which doesn't fit all needs. – Countzero May 23 '12 at 18:42
7

Try the patch in this issue : Pull filter value from an argument

It allows you to pass the value from the contextual filter to the "normal filter". There you can do your advanced conditions.

7

The comment thread starting here describes an effective workaround. MotoTribe linked to the same thread, but at the time of his question, there was no working example on that page. Now there is, as follows:

In your module, implement hook_views_api():

function MODULENAME_views_api() {
  return array(
    'api' => 3,
  );
}

Then add a new file to your module called MODULENAME.views.inc, and implement hook_views_query_alter():

function MODULENAME_views_query_alter(&$view, &$query) {
  if ($view->name == 'whatever_your_view_is_called') {
    $query->where[0]['type'] = 'OR';
  }
}

Also be aware of this caveat:

Apparently, the "Content: Published (Yes)" filter that comes with every View is considered to be a part of the same filter group as the contextual filters (group 0). So in order to make my view display only published nodes which belong to any of the groups specified by contextual filters (published AND (arg1 OR arg2 OR ...)), I had to move the "Content: Published (Yes)" into a new filter group (using the "And/Or, Rearrange" dialog). Note that the UI didn't display the "Content: Published (Yes)" filter any differently after I moved it into a new group (there weren't any other groups), but in the query object that gets passed into hook_views_query_alter() it did move it from group 0 into group 1, causing the code in the above functions to no longer affect it.

  • 1
    It is better to at least paraphrase the relevance of the references to the OP's question. Link only answers are discouraged because links may not persist. – Triskelion May 16 '13 at 0:34
  • @Triskelion: Ah, you're absolutely right. I've edited my response to fix that. – coredumperror May 16 '13 at 5:12
4

There is a module which changes handlers of Views to make contextual filters to be used with OR instead of AND: Views Contextual Filters OR

  • I want to create a view with content from "current user" + "Content from flagged users (the current user has flagged using follow flag[user])" – Umair Jan 7 '17 at 13:39
2

The situation with Contextual Filters and Filter Groups is indeed bleak. Here is thread on D.o about this toppic, which has the same solution as stated by @CoreDumpError.

https://drupal.org/node/1451218

Also, here is a module that can help resolve conflicting contextual and exposed filters... Views Filter Harmonizer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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