I have a page view with exposed filters. One of the filters is term vocabulary, and that's dropdown list, user can select only one category (term).

I want to set programmatically one term from dropdown list when I came from specific URL. I tried with views_query_alter where $category is tid.

 $set_exposed_input  = array();
 $set_exposed_input['field_category_tid'] = $category;

 if ($set_exposed_input) {

For drupal 7 you can use

$view->exposed_input[$key] = $value;

This is

$view->exposed_input['title'] = 'My title is a cat';

For a full example you can try this:

$name = 'name_of_view';
$display = 'page_1';
$alter = array('exposed' =>array('title' => 'title_value'));

$view = views_get_view($name);
$view->is_cacheable = FALSE;

  foreach($alter['exposed'] as $key => $valor){

     $view->exposed_input[$key] = $valor;


$output = $view->display_handler->preview();


Other way is using the views set_option function

$filters = $view->display_handler->get_option('filters');

foreach($alter['exposed'] as $key => $valor){
   $filters[$key]['value'] = $valor;
   $view->display_handler->set_option('filters', $filters);
  • 1
    where do you place this, in a view pre execute or something like that? – Mika A. Apr 17 '13 at 8:57
  • In the views tpl! And the full example, wherever, it has a load view function – rafinskipg Apr 26 '13 at 9:38
  • 1
    You can also add it to hook_views_pre_view – Quentin Campbell Aug 26 '13 at 15:33
  • 6
    The tpl file should be used to display rendered content, not to set business logic. Please try one of the other myriad views hooks: api.drupal.org/api/views/views.api.php/7 – aaronbauman Oct 22 '13 at 15:21
  • What about non exposed filter? I have very rare use case and I need to set filter value by custom code. – Codium Dec 6 '15 at 1:43

with Drupal 7.54 when you want to process the exposed filter programmatically, this is how you do it when you have a multiple valued field. The solution to see how to pass exposed filter is to follow how the URL is made.

Here: view_name?alias[1]=1

$view = views_get_view('view_name');

$filters = array('alias' => array('1'));


You can execute this from wherever you want. Preferably from the module file.


Set exposed filter value like this:

$view->exposed_input['field_category_tid'] = $category;

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.