Field Note

Filtering Elements in AstroJS Content Collections

astrojs collection filter
Posted on Sunday, August the 4th 2024
1 min read

Filtering Elements in AstroJS Content Collections

AstroJS content collections let us organize logically connected groups of documents. For example,

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);

// ...
friedrichkurz.me

© 2025 Friedrich Kurz

Privacy Policy