The Astro getCollection() filter is an optional second argument: a callback that receives each entry and keeps it when it returns true. getCollection('blog', ({ data }) => !data.draft) returns only published posts. The callback is typed from your collection schema, so data.draft, data.tags and id autocomplete and type-check.
Key Takeaways
- The filter runs on each entry and works like
Array.prototype.filter().- Use
import.meta.env.PRODto show drafts in dev and hide them in production builds.idholds the file path forglob()collections, so folder filters useid.startsWith().getCollection()doesn’t sort. Sort the returned array yourself.
Hide drafts in production
Add a draft field to the schema with a default, so old posts without it still validate:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: z.object({
title: z.string(),
pubDate: z.coerce.date(),
tags: z.array(z.string()).default([]),
draft: z.boolean().default(false),
}),
});
export const collections = { blog };
Then filter on it. This version shows drafts while you run npm run dev and drops them from npm run build:
// src/pages/blog/index.astro (frontmatter)
import { getCollection } from 'astro:content';
const posts = await getCollection('blog', ({ data }) =>
import.meta.env.PROD ? data.draft !== true : true,
);
Use the same filter everywhere posts are listed: the index, tag pages, getStaticPaths(), the RSS feed and the sitemap source. A draft filtered from the index but not from getStaticPaths() still gets a public URL.
Filter by tag
// src/pages/tags/[tag].astro (frontmatter)
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const posts = await getCollection('blog', ({ data }) => !data.draft);
const tags = [...new Set(posts.flatMap((p) => p.data.tags))];
return tags.map((tag) => ({
params: { tag },
props: { posts: posts.filter((p) => p.data.tags.includes(tag)) },
}));
}
const { tag } = Astro.params;
const { posts } = Astro.props;
Fetching the collection once and filtering in memory is simpler than calling getCollection() per tag. The tags and categories pages guide builds the full version with counts and pagination.
Filter by folder or language
For glob() collections, id is the path relative to base, without the extension. A post at src/content/blog/en/hello.md has the id en/hello:
const english = await getCollection('blog', ({ id }) => id.startsWith('en/'));
This is the usual way to split one collection into languages or sections without a separate schema.
Filter by date
Hide posts scheduled for the future, so you can merge them early:
const now = new Date();
const live = await getCollection('blog', ({ data }) => !data.draft && data.pubDate <= now);
On a static site, “now” is build time. A post dated tomorrow appears only after the next build, so pair this with a scheduled rebuild if you publish on a timetable.
Sort the results
The filter decides what’s included, not the order. Sort afterwards:
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
To keep this consistent, put the filter and sort in one helper:
// src/lib/posts.ts
import { getCollection } from 'astro:content';
export async function getPublishedPosts() {
const posts = await getCollection('blog', ({ data }) =>
import.meta.env.PROD ? !data.draft : true,
);
return posts.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
}
Import getPublishedPosts() in every page, the feed and getStaticPaths(), and drafts can’t leak through a page you forgot to update. More collection patterns are in the content collections cheatsheet and our content collections docs. The official getCollection() reference lists the full signature. Komal blog and editorial themes use this helper pattern for their post lists.
FAQ
How do I exclude drafts from getCollection() in Astro?
Add draft: z.boolean().default(false) to the schema and pass a filter: getCollection('blog', ({ data }) => !data.draft). Wrap it in import.meta.env.PROD if you want to preview drafts in dev.
Does getCollection() return entries in order?
No order is guaranteed. Sort the array after filtering, usually by a date field from frontmatter.
Can I filter getCollection() by multiple conditions?
Yes. The callback is plain JavaScript, so combine checks with && and ||, for example a tag, a date and the draft flag in one expression.