#Do you know how to limit the results when using GetCollection?
3 messages · Page 1 of 1 (latest)
Yes, you can limit the results when using getCollection() by using a filter function. The getCollection() function accepts an optional filter function that allows you to narrow down the results based on an entry's id or data (frontmatter) properties. For collections of type: content, you can also filter based on slug.
Here's an example of how you can filter out content entries with draft: true:
// Example: Filter out content entries with `draft: true`
import { getCollection } from 'astro:content';
const publishedBlogEntries = await getCollection('blog', ({ data }) => {
return data.draft !== true;
});
And here's an example of how you can filter entries by sub-directory in the collection:
// Example: Filter entries by sub-directory in the collection
import { getCollection } from 'astro:content';
const englishDocsEntries = await getCollection('docs', ({ id }) => {
return id.startsWith('en/');
});
You can find more information about this in the Astro documentation (https://docs.astro.build/en/guides/content-collections#filtering-collection-queries).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@agile yew kapa.ai is still learning and improving, please let us know how it did by reacting below