2

I have a view that has a "Has taxonomy term ID" contextual filter and also a nid contextual filter.

I execute this view programmatically, but I need to pass also multiple values for the taxonomy contextual filter.

I tried this:

$view->set_arguments(array($tid, $nid));
$view->execute();

and it works only if $tid is only a number.

I tried to put more than one tid using "+", but it doesn't work. (example 3+5+8)

Any idea on how I can set a multiple taxonomy argument programmatically? thank you in advance


I found a solution, but I don't like it. I removed the filter from contextual filter and I added an exposed filter for the same field. Then I changed the code in this way:

$view->set_arguments(array($nid));
$view->exposed_input['field_myfield_tid'] = $tid;  //note that $tid is an array of tid
$view->execute();
3
  • possible duplicate of Set multiple items for an argument in embeded view
    – sanzante
    Commented May 3, 2014 at 15:32
  • thank you, but I think that it is not. I have no problem in passing multiple arguments (look at: array($tid, $nid)). Here the problem is related on how to pass multiple values for the same argument ($tid = $tid1 . "+" . $tid2 . "+" etc.)
    – lorenzo
    Commented May 4, 2014 at 8:21
  • Separate multiple terms with a plus sign. I have tried it and worked. $view->set_arguments(array('23+4585+84', 123)); // 123 is the NID
    – AKS
    Commented May 4, 2014 at 17:36

2 Answers 2

0

You could also implement the "hook_views_pre_view" function in order to alter the contextual filter arguments of your view and display. In order to add multiple taxonomy values for the same argument, you can change the $args[0] element from the $args variable of the hook.
For example: $args[0] = '22+14+64';
Remember to check the "Allow multiple values" option of the contextual filter.

Take into account that altering contextual filter arguments inside the previous hook may produce troubles when using a views exposed form. Specifically, the form "action" attribute may have a wrong URL. If that issue appears, you can solve it by implementing the "hook_form_views_exposed_form_alter" function and changing the $form['#action'] element with the correct URL.

0

For achieving this requirement there are several methods , the first one is in your Taxonomy Contextual filter enable Specify validation criteria and put Filter value type on Term IDs separated by , or +,

enter image description here

with this settings the following code will works for you.

   $tid = "3,4,5"; // or  $tid = "3+4+5";
   $view->set_arguments(array($tid, $nid)); $view->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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