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 arguments list under $view-&gt;argument and can do -&gt;setArgument() but this doesn't change the set of results.

Is there an easier way of setting a specific contextual filter?
Contextual filters use query string params.  You could hard code these into the URL.  If using a Url object, you can pass a third argument ['query' => ['param1 => 'param1_value',...]].  Try setting a filter and seeing what the URL param name &amp; value is after doing so.

In hook_views_pre_build(), you can also set the filter value directly:

$view-&gt;filter['filter_name']-&gt;value = 'filter_value';


It's helpful to have Xdebug so you can see the names of existing filters.  If you don't have xdebug, try finding the correct filter name in the ->filter property, then find the desired filter value, and put 2 and 2 together.