Skip to content Skip to navigation

Views Exposed Filters Multiple Field Search

Views offers the ability to expose filters to the end user so they may filter and sort through a views listing to find what they want in a large list of content. If you have used exposed filters before you will be familiar with exposing a filter on a specific field, such as the title field, for example. But what if you want the end user to be able to search in multiple fields at the same time?

Before we dive in, if you're unfamiliar with the terminology and functionality of views in Drupal, please visit the Views module documentation.

So to create an exposed filter that can let users search multiple fields at the same time, my first thought was to create a custom module with a views_query_alter() hook that would allow me to join fields together. This is not needed, however. Views 7.x-3.x allows you to expose one filter field to the end user for multiple field searching. Views offers a 'Global: Combine filter fields' option that allows you to pick one or more fields to filter on.

Example

In a recent use case in a website we are building we had the following issue. In the screenshot below there is an exposed filter block to the left with an exposed title filter. This field allows end users to search for a title in the view listing to the right of the exposed filter block. This field works great if the user knows the full title of the content they are looking for but in practice many of these items are referred to with a short acronym. For example, the Achievement Rewards for College content item is also known as "ARCS." When using the title search field we would like the correct content to show up even if the user searches for ARCS. To do this we need a combined filter.

 

VGPE Fellowship Exposed Filter View

VGPE Fellowship Exposed Filter View

View Setup

The following example will assume that you know the basics of creating views in Drupal.

  1. The first step is to add the new combined exposed filter to your view. To do that click on the add button in the filter criteria box and wait for the overlay box to load.
  2. Once it has loaded type in the word 'global' into the search field at the top of the overlay window. You should be presented with at least one option.
  3. Select the 'Global: Combine field filter' option and click the apply button.
  4. Now the filter field has been added you will need to configure it to be exposed to the user as well as select the fields you wish to combine search on.
    Click add
    Select Global
  5. Expose the filter to the end user by checking the exposed filter checkbox.
  6. Ensure you add a title for the user that is descriptive. The description field is optional.
  7. The operator select box provides you with several options on how the search should be performed. For this example we will use the contains option as is provides us with the most results.
  8. Select all of the fields that this one filter will search. Below the fields Title and Tagline are selected. You may select more then two fields.
    More settings
     
  9. Click apply and save the view. That is all the configuration this view needs. Now you can create multiple search fields with custom results.

 

Categories: 

Comments

Thank you for your guide. Could you please provide a link to see this in action?

The search is case insensitive?

You are correct. The search is case insensitive. Try it out here: https://vpge.stanford.edu/fellowships-funding/all

Thanks, great tut, any idea about filtering that way taxonomy vocabularies?

I do not know of a way to combine multiple vocabularies with core views functionality. You may be able to combine the vocabularies on the content type using this module: https://drupal.org/project/combined_termref. From there you should be able to use views to filter on that new field. I have not used or tested this module so I am interested in how it works. Let us know.

Awesome things here. I am very glad to see your article. Thanks a lot.

Hi,

Love this tutorial - it was very helpful. However, I'm trying to combine "Webform submission data: Value" fields from the Webform module (this View consists of submitted webform values from users). Any time I try to search, it's not coming up with any results. Is this too "special" a field handler?

Thanks!

It looks like that field is 'too special'. The data from the webform submissions is not avialable to the combine field. This would be a good feature to have and should be submitted to the webform drupal.org issue queue. Without digging into the code I am not sure this can be done with the current views webform data field.

If you are looking for a powerful search option you may want to look into Search API and Search API Views. With these tools you should be able to generate a searchable index. If you go down this road let us know how it turns out.

Hi this is a great tutorial , thanks for sharing..
Can you guide , as how to relate the dropdown list while filtering.
For example
I have following fields.
1.Continent
2.Countries
3.Cities
When in Continent "Europe" is selected , the next drop down should only show list of countries of Europe & and Countries if "France" is selected , the next drop down should only show cities of france, excluding rest of the cities of the world.
regards
Ahmed

Hello Ahmed,

Conditional input fields are a trickey thing. There is a module to help support you in this kind of interactivity for content types called Conditional Fields but it won't help you with views. The closest thing I found for what you are asking for in a contributed module the Views Dependent Filters module. I have not used the Views Dependent Filters module so I cannot make a comment to its status or value. I would be interested if someone has an approach for this outside of custom code.

 

Thanks for your reply , I digged in dependent filters, but that's not what I am looking for.

I am still in search for the said solution.

Thank you once again for your reply & a great informative tutorial on exposed filters.
regards
Ahmed

This was a great tutorial and I managed to learn how to do the filters, however could you perhaps have a tutorial on how to change the search results? I still am using the default drupal search results with the plain table, however you seem to have managed to make it more aesthetically pleasing, and I have no clue how to change to appearance to make it look like yours.

Thanks
Jonathan

Hello Jonathan,

That is a great suggestion for a blog topic. I will add your suggestion to our list of posts to write. In the mean time, one of our other favourite modules has the ability to change the display of the search results. Display Suite can modify the search page in all sorts of ways. I found a good tutorial on youtube that will hopefully get you started: https://www.youtube.com/watch?v=rn3e91C_2jw

Good luck.

Hello and thank you for this great tutorial. I know I'm late to the party, but maybe someone can help me out. I would like to know (I need just an input, I'm totally lost here) how to combine ALL the filters and the full text search together. You have a "Search by title" and then 4 filters, with ONE "Go" and "Reset" button. How can I achieve this' Many thanks from Italy!

If you are looking for just one search field that searches everything, combined filters is not the best way to go. I would suggest you look in to using something like the search_api module in order to make what you suggest work.

Good luck.