Views filter

A filter in a Drupal View is a condition each item must meet to be listed, such as content type or published status. Visitors can set it if exposed.

Learn DrupalDrupal glossary

A filter in a Drupal View is a condition that each item must meet to be listed. It can test the content type, the published status, the value of a field or the words in a title. Items that fail the test are left out of the results. The Views editor lists filters in the Filter criteria section. They never change the order of the results; sort criteria do that.

How filters work in Drupal 11

To add one, open the view's edit screen, go to Filter criteria and click Add. In the For field, choose All displays or This page (override). Select one or more filters, narrowing the list with the search box or the Category menu, and click Apply. The Configure Filter Criterion dialog opens. Pick an operator, enter the values it needs, and click Apply again.

In code, every filter is a plugin that extends FilterPluginBase. Its options include operator (the code default is =), value, group and exposed (default FALSE). The operators depend on the filter type. A text filter offers, among others, Is equal to, Contains, Contains any word, Starts with, Length is shorter than and Regular expression.

With several filters, And/Or Rearrange opens a form where filters sit in groups. The first group is called the default group. The button Create new filter group adds more, each group has its own operator, and once there are two or more groups a separate operator joins them.

A filter that can be exposed shows an Expose filter button. Exposing turns it into a form control for visitors. The expose settings include a label, an identifier, Required, Remember the last selection and Allow multiple selections.

Example

A resources page should list articles and case studies, but only published ones. Add a filter on the content type, choose the operator Is one of and select both types. Then add the Published status or admin user filter. If the page also needs a search box, add a title filter with the operator Contains and expose it, so visitors can type a word.

Common mistakes

  • Filtering a fixed value when the value should come from the page. If the list must change with the URL, for example per author, use a contextual filter.
  • Filtering on floating-point numbers. The Drupal.org documentation warns that comparing floats is generally a poor idea.
  • Using an identifier Drupal rejects. An exposed identifier cannot be empty, and names such as q, value and destination are not allowed.
  • Hiding unpublished content from editors. A plain "status is published" filter hides drafts from everyone. The core filter Published status or admin user hides unpublished content only from users who cannot view it.
  • Changing every display. The same For choice that adds a filter decides whether other displays get it too.

For marketing and content teams

Describe the rule in plain words: "all published case studies from the last year" or "articles about either security or performance". The second is an Or between two conditions, which changes how the filters are combined, so say it that way. Also say whether readers should be able to narrow the list themselves; that decides which filters are exposed.

With DXPR

When DXPR Builder places a view on a page, the list appears with the filters the view defines. The view's filter form shows above the list only if you switch on Show exposed filters in the element settings, and the view needs Use AJAX set to Yes. See DXPR Builder.

Sources

Questions about Views filter

A filter has its value fixed when you build the view, or chosen by the visitor if you expose it. A contextual filter takes its value from the context when the view runs, for example from the URL. See contextual filters.

Open And/Or Rearrange in the Filter criteria section and set the operator of the default group to Or. To mix And and Or, click Create new filter group, move filters into it, and set the operator of each group and the operator to use on all groups.

Yes, if you expose it. An exposed filter appears as a form above the results, with a label and an identifier that you set. Some filters cannot be exposed; the Published status or admin user filter is one of them.

Related terms

All Drupal terms from A to Z