How to set programmatically multiple arguments for views contextual filters

Solutions

If you're simply looking to render the view with its arguments (and don't need to do any more work on it), you can just call views_embed_view(). It's called like so:

<?php print views_embed_view($view, $display, $arg1, $arg2, ...); ?>

The $view and $display are the machine names of same. See the above-linked documentation for details.

First you should set this option: View → Advanced → Your Contextual Filter → More → Allow Multiple Values. Then when passing array should be with '+', so, your example:

$array = array('3250+32');
$view = views_get_view('myview');
$view->set_display("myblock");
$view->set_arguments($array);
$view->pre_execute();
$view->execute();
$content = $view->render(); 

Another one approach, which proved to be quite useful:

$array = array('3250', '32'); 
$view = views_get_view('myview');
$view->set_display("myblock");
$view->pre_execute($array);
$view->execute();
$content = $view->render();

pre_execute() method of view object accepts arguments, that will be passed to corresponding contextual filters. E.g. '3250' will be passed to the first contextual filter, '32' - to the second one.

More info about method here.

Tags: Views

Similar questions

How do I programmatically set contextual filters?
I need to set a contextual filter programmatically and almost every view I have will have this filter. However, some wont and will have other contextual filters instead. I have tried following the examples in here but this will just set the first argument to be my filter value regardless of what field is it for. In views_pre_build I get the keyed a...
How to show nodes of a content type using contextual filters as filters?
I have a profile page for each user where it will show all nodes he has created for each content type. For instance, Autos content type will only list all autos he has authored. I'd like to be able for the user to access it using this path : http://localhost/mysite/myreports/autos and will show his autos, .../myreports/jobs will show his job postin...
Pros and cons of exposed filters versus contextual filters
I need to do some complicated filtering on my View and it seems that contextual filters lack the operators (e.g. between, greater than) available within the exposed filter configuration. However, I don't want to user to be able to see or amend any filter widgets. I can achieve this by setting the exposed filters to appear as a block and NOT assigni...
views_query_alter change to operator OR for contextual filters and normal filters
My question is rather simple and I have searched quite a bit for the answer with no result. My view produces below SQL (simplified): All I want to do is to change that AND between the contextual filter and normal filter. I have tried to change all ANDs (including set_group_operator()) to OR and it does not change my AND. Also need to mention it is ...
Exposed form for contextual filters when using non-taxonomy filters?
I have a View with some contextual filters which works great. I also need to provide my users with some way to change the filters once they're on the page. I've tried using exposed filters but I'd rather use path arguments over query string parameters and there is the conflicting issue between exposed filters and contextual filters which the Views ...
Is it possible to combine contextual filters with regular filters?
I would like to take my standard taxonomy term view and add a "meta" category that would show up in addition to the term selected. The result should be that items in the meta category show up as well as those in the term being viwewed. I tried adding the meta category as a regular filter or an additional contextual filter, but there's no way to add...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.