Views lets you filter query results using custom fields. This way, you can load the exact content you need, depending on custom field values.

You can filter by as many custom fields as you need, filter by hard-coded values or let visitors choose filter values.

On this page:

  1. Inputs reference
  2. Examples
  3. More reading

1. Inputs Reference

In the Query Filter section of a View, click Add a filter. You will see different filter options. Choose a custom field and you will get to this dialog:

Filtering Views Query by Custom Fields
Filtering Views Query by Custom Fields

This dialog includes a wealth of filtering options. Let’s go through them.

Comparison function

The comparison function is the function that Views will use to compare between the custom-field value and the filter value.

Values for this field correspond to the MySQL comparison functions:

Comparison functionArgumentsPurpose
=Single valueChecks if the custom field value exactly matches the compare value
!=Single valueChecks if the custom field value doesn’t match the exact compare value
>Single valueChecks if the custom field value is greater than the compare value
>=Single valueChecks if the custom field value is greater than, or equals, the compare value
<Single valueChecks if the custom field value is less than the compare value
<=Single valueChecks if the custom field value is less than, or equals, the compare value
LIKESingle valueChecks if the custom field value is similar to the compare value, using MySQL LIKE operator
NOT LIKESingle valueChecks if the custom field value is not similar to the compare value, using MySQL NOT LIKE operator
INA list of valuesChecks if the custom field value is within the list of compare values
NOT INA list of valuesChecks if the custom field value is not within the list of compare values
BETWEENTwo valuesChecks if the custom field value is between the two compare values
NOT BETWEENTwo valuesChecks if the custom field value is not between the two compare values

Compare value type

In order for comparison functions to work correctly, you need to tell Views what’s the data type you’re comparing to. For example, if your data type is a string, you must compare it to another string, to get the correct results.

Value types can be one of these:

Data typePurpose
CHARTexts
NUMERICNumbers
BINARYTrue / false
DATEDates
DATETIMEDate and time
DECIMALDecimal values
SIGNEDSigned decimal values
TIMETime
UNSIGNEDSigned decimal values

Source for compare value

You can filter according to constants (values that you determine when setting up the View), by time functions, by values that visitors will specify in custom searches, by time functions or by values set by Views shortcode attributes.

Value sourceDescriptionPurpose
ConstantA value that you specify when setting up the ViewFor example, to choose only ‘featured’ posts
URL parameterThe value for the filter is set in the URL to the pageThis will let you create custom searches filters for Views, where visitors can control what the View will show.
Shortcode attributeThe value for the filter comes from an attribute in the View shortcodeYou’ll be able to insert the same View in different places and control content filtering via the View Shortcode.
NOWThe current time (in seconds)Lets you filter content relative to the current time
TODAYToday’s time (doesn’t change from hour 0 to hour 23)Lets you filter content relative to the current day
FUTURE_DAYTODAY + a number of days set in the second fieldLets you filter content relative a certain number of days from today
PAST_DAYTODAY – a number of days set in the second fieldLets you filter content relative a certain number of days before today
THIS_MONTHCurrent month (doesn’t change during the month)Lets you filter content relative to the current month
FUTURE_MONTHCurrent month + a number of months set in the second fieldLets you filter content relative to a future month
PAST_MONTHCurrent month – a number of months set in the second fieldLets you filter content relative to a past month
THIS_YEARCurrent year (doesn’t change during the year)Lets you filter content relative to the current year
FUTURE_YEARCurrent year + a number of years set in the second fieldLets you filter content relative to a future year
PAST_YEARCurrent year – a number of years set in the second fieldLets you filter content relative to a past year
SECONDS_FROM_NOWCurrent time + a number of secondsLets you filter content relative to a future time, calculated in seconds from now
MONTHS_FROM_NOWCurrent time + a number of monthsLets you filter content relative to a future time, calculated in months from now
YEARS_FROM_NOWCurrent time + a number of yearsLets you filter content relative to a future time, calculated in years from now
These values represent the beginning of the source. For example, THIS_MONTH returns a value representing the beginning of the current month. In case that you want to get results from the whole current month, you should use a comparison like THIS_MONTH <= post_date < FUTURE_MONTH(1).

2. Examples

Get all posts with 'House Type'='villa'
Get all posts with ‘House Type’=’villa’

Get all posts that 'House Type' is 'villa' or 'apartment'
Get all posts that ‘House Type’ is ‘villa’ or ‘apartment’

Get posts published the current month
Get posts published the current month

3. More Reading

  • Need to filter by date and time? Real more about Views date filters.
  • Want to allow visitors to filter content? Learn about custom searches.