Filtering Elements in AstroJS Content Collections
AstroJS content collections let us organize logically connected groups of documents. For example,
- blog posts
- product pages
- newsletters
After defining a collection blog
, we may get all elements in the blog collection by using the getCollection
function. This function, conveniently takes a filter as the second argument. So, consider we have the following post frontmatter
title: the post
hidden: true
then filtering all hidden posts may be done like this
import { getCollection } from 'astro:content';
const blogEntries = await getCollection('blog', ({ data }) => ! data.hidden);
// ...