Back to top

Search multiple Drupal Fields at the same time—Combine fields filter

Combined fields filter in Drupal 8 Views

The easiest way to create a search in Drupal that would automatically search more than one field is through the Combine fields filter in Views. More complex solutions include the Search API module, or Solr framework.

It's not possible to combine just any field with the Combine fields filter, but most of the fields that store content you want searched can. That includes Taxonomy Terms; they can be included in the combined search with the help of Relationships.

Configure the Combine fields filter

Add Fields before adding the Filter

The first step in configuring the Combine fields filter is to add the fields you want to be searched to your View. If you don't want the Fields to be visible in your results you can Exclude from display, but they must be present on the Fields Views.

Configure the Operator and select Fields

Once you have the desired fields available you can add the Combine fields filter from the Global Category and configure it to use those fields:

Combined fields filter Configuration

Hold Ctrl while clicking on different Select Options to select more than one option from the list.

The Select list will show all the present fields that are available to be used in the Combine fields filter. If you have some fields present in your Views that aren't showing up it usually means they aren't supported out-of-box but can be made to work using Relationships as shown further below in this article.

Don't forget to set the Operator to an option that makes sense in your case. When searching through text fields (Title or Body) it makes sense to use the Contains or Contains any word operator. Using the Is equal to Operator makes sense when searching through fields with short well-known values such as numbers.

This way you can build a search result page listing all the content from your site that has the “curious cat” expression somewhere in the Title or Body. In this case you'd use the Contains Operator.

Expose Combine fields filter—add Label and Description

More frequently you'll want to Expose the Filter to allow your users to enter whatever expression they want to search for. It is always good practice to add descriptive Labels and Descriptions to your Exposed filters to make it easier for your users to understand the tool.

In this example the Operator is set to Contains so we educate the user we're after the exact expression:

Exposed Combined fields filter

Search through Taxonomy Term names

Taxonomy term names aren't available out-of-box

Content Fields that mark nodes with Taxonomy terms (Entity Reference Field type) store only the term ID, making that number is available for immediate use within Views. For term names you have to add the Relationship towards the taxonomy term. This is because that info is stored in a different table in the database, and Relationships introduce data from other tables into your Views.

When creating Views you choose which type to create. This basically chooses which database table to base the Views on. Initially, the created Views Display can only pull the data from that base database table.

For example, if you create Content type Views you will be able to use the various Content Fields inside that View, but you won't be able to use Fields from the User Profiles.

By adding a Relationship towards another (related) table in the database we can display information about other (related) entities in our Views.

Need more help? Schedule a free 15 minutes evaluation callContact me

If we add the Entity Reference fields (that store only term ID) to our Views, and then try to use the Combine fields filter we'll see that those fields aren't on the available list. Even though the Entity Reference fields come with special Formatters that allow us to display the term name instead of the term ID, this only affects the field output in the Views results and it can't be combined with other Views features.

To be able to search through Taxonomy term names with the Combine fields filter we need to add directly the term Name field. This can be accomplished by adding the Relationship for the Entity Reference field.

Add Relationships for Taxonomy Entity Reference fields

To get the information about Taxonomy terms in our Content Views we add the Relationship for the Entity Reference field that stores the terms we want to search through. They will be listed in the Content Category and will be named similar to “Taxonomy term referenced from field_myfield”:

add Relationship towards Taxonomy Term

You need to add Relationships for each field you want to have information available in your Views other than term ID.

Add Term Name fields

Once the Relationships are added new fields will become available in our Views, fields about the Taxonomy terms that weren't there before adding the Relationships. One of those fields is the Name:

add Taxonomy term Name field

This field has to use the Taxonomy ER field Relationships we added previously. Add as many Name fields as you want to be searched simultaneously, making each one use a different Relationship:

configure Taxonomy term Name field

You can Exclude from display if you only want these fields to be used for the Combine fields filter, without appearing in the results. You can use the regular ER fields to show in the results, although you could also use the Link to the Taxonomy term on the Name field as a replacement for the ER fields simplifying the configuration.

Add Combine fields filter

With the Names fields available we are ready to add the Combine fields filter. This time the Taxonomy term Names will be listed as choices:

Combine fields filter Taxonomy configuration

Expose the filter if you want the users to search through your Taxonomy fields in one go and give it meaningful Label and Description.

Combined fields filter in action

In the following screenshot, we can see the word “blue” being searched through both “Color” and “Music” fields, returning results when a hit was found in either of those fields. Since the word “blue” is contained in the word “Blues” the nodes marked with the Blues music are also matched:

Combined fields filter in Drupal 8 Views Taxonomy

If we enter more than one word we get results for either of them if the Operator is set to Contains any word:

Combined fields filter in Drupal 8 Views Taxonomy Contains any word

If we set the Operator to Contains all words the condition is more specific but it still searches through both fields as if they are one “bucket” to draw from:

Combined fields filter in Drupal 8 Views Taxonomy Contains all words

Here we see the node that is marked with both the searched words, even when those words are spread across more than one field.

Need any help with this? Or with any other Drupal problem? Contact me and I'll summon all my skills and Drupal might to solve it for you.
What did you think of this article?