Skip to main content

Setting the Default Value for Views Exposed Filters

File under

Sometimes the help you’re looking for is in the unlikeliest of places, which, in this case, is – ironically – the likeliest of places. And yes, that is all proper punctuation and grammar.

Needing to know how to set the default value for an exposed filter in Views (there’s no way to do this via the UI that I can find), a Google search turned up the Views API docs. MIND = BLOWN.

Forcibly insert this into your custom module:

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  if (!isset($form_state['view']->exposed_input['<filter_field>'])) {
    // OK to set the default value
    $form_state['input']['<filter_field>'] = '<new_default_value>';
  }
}

Where <filter_field> is the name of the exposed filter and <new_default_value> is the key of the value you want as the default. You’ll also want to put some code in there that checks whether you’re on the right view, else you’ll be setting this default value all over the place.

You can also read the API docs. Seriously impressed that there’s an actual Views API doc page devoted to this. Thanks Merlin!

Thanks

Just wanted to thank you for posting this solution. I searched for a while before coming across your post. It solved the issue for me nicely, so thank you.

Thanks! It was useful!

Thanks! It was useful!

Thanks, BUT....

This loads currect filtered output on load, BUT, when the user tries to choose a different value from the same exposed dropdown, it re-directs to whatever the default filter set in the path (I am using AJAX output on the view page)

useful info

thanks,

this was useful info,
I needed this to translate the default value via t() function.
but why doesn’t work with the $form remains a mistery to me ?…

Regarding the latest comment above, this is because you removed the ” if (!isset($form_state[‘view’]->exposed_input[”])) {“

regards

Thank you very much. It

Thank you very much. It really save couple of hour, even if i have wasted few hours.

This is great and saved me

This is great and saved me some heartache! Thanks.

In case anyone else Googles

In case anyone else Googles their way here: there is a way to set an exposed filter’s default via the GUI. When configuring the filter:

  1. check the box next to the option you want as your default
  2. be sure that the “Limit list to selected items”, below that list, is *not* checked
  3. If your view uses ajax (see “thanks, BUT” above), be sure to check the “Remember the last selection” box and all appropriate roles’ sub-boxes

And all should be well.

Yes, sub-boxes is a word, cause I just made it one.

Ahhhh, finally… thnx!

Ahhhh, finally… thnx!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <img> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h1> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options