Skip to main content

pin-query

When to use​

Pin a CDA query (filter + sort + limit) to a section or template so Repeater sources iterate dynamic content like top 3 featured products. Use Studio UI, driven through Playwright per drive-studio-canvas-ui. Don't hand the clicks to the user or switch to the API unless they ask; that's author-composition-via-api.

Use when a section or template needs to iterate a dynamic list of entries (top 3 featured products, latest 5 articles, sorted catalog). Phrases: "pin a query", "iterate latest entries", "filtered list on a section or template", "filtered list on freeform". Re-runs every render so content stays fresh. For single-entry pinning use pin-entry.

Mandatory auth preflight: settle the credential before the first API call. Resolve it OAuth-first per authenticate-cma: CS_OAUTH_ACCESS_TOKEN, else the Contentstack MCP's stored session: Read that skill for where it is, even with no MCP connected. Never ask the user for a session authtoken. If nothing resolves, or a refresh fails with 400 invalid_refresh_token, hand them ! CONTENTSTACK_REGION=<code> npx @contentstack/mcp --auth (it needs a TTY and a browser, so it cannot be run for them) and wait. 403 error_code 316 is a valid credential aimed at another org: fix the org or the api_key, do not re-authenticate.

Pin a CDA query to a section or template

Prerequisite: the site needs @contentstack/studio-react 1.3.0 or later (it brings @contentstack/studio-client 1.5.0, the first that runs queries). Check with npm ls @contentstack/studio-client: 1.5.0 or later. Pinned queries work on sections, connected templates and freeform templates. The Data tab appears only when Enable Freeform Feature is on (Studio, then Settings, then Configuration). The standard pattern is a section with a pinned query (for example Latest 3 posts), placed in connected templates; see build-section § Pinned-data sections.

Context​

A section or template can pin a CDA query so a Repeater iterates a dynamic list of entries, for example the top 3 featured products or the latest 5 articles. A query pinned on a section is stored on the section and travels with it into every template that places it; a template's Data tab doesn't list it. The query (filter + sort + limit) re-runs on every render, so content stays fresh as authors publish new entries. Reference doc: docs/15-build/15-data/15-add-a-query.md. Pinned queries differ from pinned entries: a pinned entry returns a single record (bind directly to template.*), while a pinned query returns a list and must be consumed by a Repeater whose items resolve against repeater.*.

Task​

  1. Open the composition that will own the query: normally the section (Sections tab, then open it). If the right panel has no Data tab, turn on Enable Freeform Feature.
  2. Deselect any component on canvas (click empty canvas area) so the right-panel Data tab shows the composition's own pins, not component-scoped ones.
  3. In the right panel, open the Data tab, find the Pinned Queries section, and click Create Query.
  4. In the New Query modal, fill the required Name and Content Type fields.
  5. Configure the query one of two ways:
    • By hand: set the filter, order, and limit fields directly.
    • Natural language: type a prompt in the Tell me what data you need box. Studio converts the prompt to a stored CDA query at save time. This conversion is one-shot: it is NOT re-evaluated at render. To tune the query later, re-open the modal.
  6. Save the query. It appears in the Pinned Queries list.
  7. Use the query on canvas: drop a Repeater component, click Bind items in Properties, and pick Pinned Queries, then <name>, then Entries. Count beside it is the number of results, bindable to a number prop.
  8. Drop a card or item component inside the Repeater body. Its bindings resolve against repeater.*: each iteration represents one entry from the query result.
  9. If the iterated content type contains references (single-CT or multi-CT), pair the Repeater with a Condition Block for each reference field. See use-condition-block.

Inputs needed from the user​

InputPrompt
compositionNameSection or template that owns the query
contentTypeUidContent type the query returns
filterExpressionFilter (e.g. featured = true) or natural-language prompt
sortBySort spec (e.g. price asc) or blank
limitResult limit (e.g. 3, 5, 10)

Acceptance​

  • In the owning section's or template's right panel, the Data tab's Pinned Queries list shows the new query by name.
  • Dropping a Repeater on canvas and opening Bind items shows the query under Pinned Queries, with Entries and Count under its name.
  • After binding the Repeater and adding a child card with repeater.* field bindings, the canvas (with Repeater Preview Mode on) renders one card per returned entry, respecting filter, sort, and limit.
  • Publishing a new entry that matches the filter causes it to appear in the rendered list on the next render, no template edit required.
  • Placing the section in a connected template renders the query results there with no per-template setup.

Common pitfalls​

PitfallWhy it bitesFix
Leaving filter and sort blankReturns an unbounded, arbitrarily ordered result setAlways set at least a filter or a sort plus a sensible limit
Expecting the natural-language prompt to re-evaluate at renderThe prompt is converted to a stored CDA query once at save time. Render uses the stored query onlyTo change query behavior, re-open the Pinned Query modal and edit (or re-prompt)
Binding a Repeater to a single-result queryA Repeater expects a list. One entry is wasted scaffoldingUse pin-entry for single-entry pinning and bind directly to template.*
Binding a prop straight to one entry's field (Entries → <entry> → Title)It binds a position (entries.0.title), not that entry, and the picker warns Showing One Item OnlyFine for "the newest post's title". For every entry click Wrap in Repeater; for one fixed entry use pin-entry
Selecting a component before opening the Data tabTab shows component-scoped pins, not the composition's ownClick empty canvas to deselect, then open Data tab
Pinning the query on each template instead of the sectionEvery template must repeat the setup, and they driftPin it once on the section
Forgetting a Condition Block for references inside the RepeaterReferences (both single-CT and multi-CT) inside a Repeater require a Condition Block to resolveWrap each reference field in a Condition Block. See use-condition-block
Binding child fields to template.* instead of repeater.*template.* resolves to template-level pins, not the current iteration itemInside a Repeater body, child bindings must use repeater.<field>

Report a problem with this page