Skip to main content

Query filters

Look up the YAML you can write in Input Filters, the filter box on step 2 of a query dialog. Use this page when you write or fix a filter. To create a query, see Add a query.

The example on this page is the Latest posts query on the Blog post content type in the Blog project. Its entry AI 101 has the slug ai-101 and the URL /blog/ai-101.

Top-level keys in Input Filters​

Input Filters is YAML with two top-level keys.

KeyWhat it sets
paramsHow many entries come back, how many to skip, and the sort order.
queryConditions on fields, in Contentstack's query form.

The params keys in Input Filters​

In params, you can set limit, skip and one sort key, asc or desc, with a field UID as its value.

KeyWhat it does
limitHow many entries to return.
skipHow many entries to pass over first.
asc or descSorts by one field UID.

Latest posts looks like this.

params:
limit: 3
desc: created_at

Query conditions in Input Filters​

query sets conditions on fields, in Contentstack's query form. Studio sends it as written, and the editor shows the message Contentstack returns for anything it rejects.

Example Input Filters​

These examples use the Blog post content type.

To getFilters
The 3 newest postsparams with limit: 3 and desc: created_at
Every post except AI 101query with slug and $ne: ai-101
Posts on a topicquery with taxonomies.topic and $in: ["{{url.params.topic}}"]

Values Studio replaces​

Studio replaces a filter value written as {{...}} when the query runs.

RuleDetail
One expression per valueWrite one expression as the whole value. Studio ignores text around it.
QuotesStudio adds quotes around a bare {{ }} value for you.
Where it worksExpressions in params, such as skip, resolve in the Studio table but not on your site.

The page URL​

{{url.params.topic}} reads the topic parameter from the query string of the page URL. On /blog/ai-101?topic=ai, it reads ai.

Where the query runsWhich URL it reads
The editorThe canvas page's URL.
Your siteThe visitor's URL.
A server-rendered routeThe request's query string, which you pass as searchQuery. Without it, url.params is empty.
A client-rendered routeThe browser's query string. Studio reads it itself.

For searchQuery, see Fetch a composition.

Time values in a filter​

Time values are:

  • {{now}}
  • {{today}}
  • {{this_week}}
  • {{this_month}}
  • {{now - 30 days}} or {{now + 7 days}}

The units are day, days, hour, hours, month and months. Weeks aren't supported. {{this_week}} starts on Sunday. Date values use the time zone of the machine that runs the query.

Suggestions in the filter box​

Typing {{ in Input Filters opens a list with two groups.

GroupWhat it lists
Dynamic ValuesOnly the parameters in the canvas page's URL. It's missing when there are none.
Time Expressionsnow, now - 30 days, now - 7 days, today, this_week and this_month.

The suggestions list in Input Filters, with Dynamic Values and Time Expressions open.

Keys you can't set​

You can't set include, only or except. Studio removes them if you type them under params. Studio then shows "The only, except, and include params are removed from Input Filters because they are not supported here." The notice shows under the filter box, so you see it only with Input Filters open. Studio adds the reference includes your bindings need, so you don't set include.

After you edit the YAML​

What you seeWhy
Save is disabled.You haven't clicked Re-fetch. The Re-fetch button appears in the box after you edit the YAML.
A message under the box, such as "Indentation error at line 3, column 5. Check that spaces are aligned correctly." The YAML is invalid. The message appears after you click Re-fetch, not while you type.

On your site, a query that comes back empty logs a warning in development, or in production with debug: true. QUERY_RETURNED_NO_ROWS means the filter matched no entries. QUERY_REQUEST_FAILED means the request didn't complete, so the filter isn't the cause. See Warnings about content that didn't load.

To create or edit a query, see Add a query.

Report a problem with this page