export const ChildSchema = Config.document('Child', {
fields: {
title: Field.text('Title'),
// A multiple entry field to link related filters or categories
filters: Field.entry.multiple('Filter(s)', {
condition: { _type: 'FilterItem' }
}),
// Main content area
blocks: Field.text('Content')
}
})export const BlogPostSchema = Config.document('Blog Post', {
fields: {
path: Field.path('Path'),
title: Field.text('Title'),
description: Field.text('Description'),
publishedDate: Field.date('Published Date'),
// Used for the "Related Content" example
tags: Field.list('Tags', {
schema: Field.text('Tag')
})
}
})This example shows how to retrieve the most recent entries while implementing a reusable pagination logic. By using constants for pageLength, you keep your query logic clean and easy to maintain.
const pageLength = 10
const pageIndex = 0 // Start at the first page
await cms.find({
type: BlogPost,
// Sort by date to get the newest items first
orderBy: {
desc: BlogPost.publishedDate
},
// Calculate offset based on the current page
skip: pageIndex * pageLength,
take: pageLength
})Use this approach when you want to retrieve entries that are linked to one or more specific items. This is particularly useful for features like "Filtering by Category" or "View all posts by these Authors."
const filterIds = [
'abc123',
'def456'
]
await cms.find({
type: ChildSchema,
filter: {
filters: {
includes: {_entry: {in: filterIds}}
}
}
})If you need to find entries that contain all specified references simultaneously, you can map over your IDs using an and operator. This ensures every single link in your list must be present for a match.
const filterIds = [
'abc123',
'def456'
]
await cms.find({
type: ChildSchema,
filter: {
and: filterIds.map(id => ({
filters: {includes: {_entry: id}}
}))
}
})This pattern demonstrates how to find related content by comparing shared references. First, we retrieve the current entry to extract its linked metadata. Then, we use those IDs to query other entries of the same type, ensuring the current item is excluded from the results to provide a clean list of recommendations. This leverages Next.js routing params.
type ChildProps = {locale: Locale; url: string}
export default async function Child({locale, url}: ChildProps) {
const page = await cms.first({locale, url, type: ChildSchema})
if (!page) return
const filterIds = page.filters.map(filter => filter?._entry).filter(Boolean)
const other = await cms.find({
type: ChildSchema,
id: {notIn: [page._id]},
filter: {
filters: {
includes: {_entry: {in: filterIds}}
}
},
select: {title: Query.title}
})
}