getCollection()

getCollection() loads all entries of a content collection, with optional filtering.

Since Astro 2.0 Spec ↗

Syntax

const posts = await getCollection('blog')

Returns

Promise<CollectionEntry[]> — A promise resolving to an array of typed collection entries.

Examples

---
import { getCollection } from 'astro:content';
const posts = (await getCollection('blog', ({ data }) => !data.draft))
  .sort((a, b) => +b.data.date - +a.data.date);
---
<ul>
  {posts.map(p => <li><a href={`/blog/${p.id}`}>{p.data.title}</a></li>)}
</ul>
Output
Renders a date-sorted list of non-draft blog posts.

Notes

Imported from astro:content. The optional second argument filters entries at build time. Entry shape is validated against the collection schema. Use entry.id for routing in modern Astro.

See also