Headless CMS > Advanced Filtering
Advanced Filtering
Learn how to use AND and OR conditionals to build complex filters when querying Headless CMS entries.
- How to use
ANDandORconditionals when filtering entries - How nested
AND/ORqueries work - How to combine both conditionals in a single query
Overview
Both AND and OR conditionals are arrays of filter conditions. The available filter keys depend on the fields defined on the model you are querying. All user-defined field filters use the values. prefix (e.g. values.title_contains).
The examples below use the Webiny SDK. The where object is passed directly to sdk.cms.listEntries().
TheANDConditional
AND requires all conditions in the array to match. It behaves the same as placing filters at the root of where, with the added ability to repeat the same operator multiple times (which is not possible at the root level).
SimpleANDExamples
Search for Entries Where the Title Contains Both "Headless" and "Cms"
At the root level you can only use values.title_contains once. Wrapping conditions in AND lets you apply the same filter key multiple times:
Equivalent condition: (values.title contains "headless" AND values.title contains "cms")
Combine a Root-Level Filter WithAND
Root-level filters and AND are applied together — all must match:
Equivalent condition: (values.category = "cat-id-1" AND values.title contains "headless" AND values.title contains "cms")
ComplexANDExample
Search for articles that:
- are in category
cat-id-1 - have both “headless” and “cms” in the title
- are authored by one of three authors
- were created in 2022
Equivalent condition: (values.category = "cat-id-1" AND values.title contains "headless" AND values.title contains "cms" AND (values.author in [...] AND createdOn between 2022))
createdOn is a system meta field available on all entries. It does not use the values. prefix.
The same query can be flattened when there is no need for repeated keys:
This produces the same result as the nested version above.
TheORConditional
OR requires at least one condition in the array to match.
SimpleORExamples
Search for Entries Where the Title Contains "Headless" or "Cms"
Equivalent condition: (values.title contains "headless" OR values.title contains "cms")
Multiple Filters Inside OneORBranch
When an OR branch contains more than one filter, all filters in that branch must match (implicit AND within the branch):
Equivalent condition: ((values.title contains "headless" AND values.category = "cat-id-1") OR values.title contains "cms")
ComplexORExample
Search for articles that match any of:
- title contains “headless”
- title contains “cms”
- category is
cat-id-1orcat-id-2
Equivalent condition: (values.title contains "headless" OR values.title contains "cms" OR (values.category = "cat-id-1" OR values.category = "cat-id-2"))
MixingANDandOR
ORat the Root With NestedANDandOR
Search for articles that match any of:
- title contains “headless”
- title contains “cms”
- title contains both “webiny” and “serverless”, and was created in January 2021 or January 2022
ANDat the Root With NestedORandAND
Search for articles that match all of:
- title contains “headless”
- title contains “cms”
- title contains “webiny” or “serverless”, or was created in January 2021 or January 2022
ORandANDBoth at the Root Level
Search for articles that:
- are written by author
author-1OR are in categorycat-id-2 - AND have both “headless” and “cms” in the title
Equivalent condition: ((values.author = "author-1" OR values.category = "cat-id-2") AND (values.title contains "headless" AND values.title contains "cms"))
AND and OR conditionals can be nested indefinitely, but deep nesting may result in performance issues — particularly on DynamoDB-only deployments. Keep nesting shallow where possible.