How to set the views exposed filters default value programmatically? I have a view which has a term reference exposed filter.

I want to set the default values of this exposed filter programmatically based on a certain condition.

I have tried the following code but the values are not selected in the filter.

function example_views_pre_build(&amp;$view) {
  if ($view-&gt;name == 'events') {
    $tids = array(5, 6);
    foreach (tids as $tid) {
      $view-&gt;display[$view-&gt;current_display]-&gt;handler-&gt;handlers['filter']['field_category']-&gt;value[$tid] = $tid;
    }
  }
}


AND

function example_views_pre_view(&amp;$view, &amp;$display_id, &amp;$args) {
      if ($view-&gt;name == 'events') {
        $tids = array(5, 6);
        foreach (tids as $tid) {
          $view-&gt;display[$view-&gt;current_display]-&gt;display_options['filters']['field_category']-&gt;value[$tid] = $tid;
        }
      }
    }


I also tried with form_alter, in form alter the values gets selected in the filter but then it does not display correct results after removing/applying again the filter.
function example_views_pre_build(&amp;$view) {
    if ($view-&gt;name == 'VIEW_NAME') {
        $view-&gt;display[$view-&gt;current_display]-&gt;handler-&gt;handlers['filter']['field_country_tid']-&gt;value = array(55, 91);
    }
}


This code worked for me. Check your views module version as well. I am using Views 7.x-3.14.