🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro Reference Collections With reference()

Link Astro collections with reference(): connect posts to authors and related posts in the schema, then resolve them with getEntry() and getEntries() in pages.

#Astro#Content Collections#reference()#Schema

To reference collections in Astro, use reference('collectionName') inside a collection schema. A frontmatter field like author: komal is then validated and stored as { collection: 'authors', id: 'komal' }, and you resolve it to the full entry with getEntry() (one reference) or getEntries() (an array). It’s how you connect blog posts to authors, products to categories, or posts to related posts without copying data between files.

Key Takeaways

  • reference() comes from astro:content and takes the name of the target collection.
  • In frontmatter you write only the entry id; Astro turns it into a { collection, id } object.
  • References aren’t resolved for you. Call getEntry() or getEntries() where you need the data.
  • Handle a missing target: getEntry() returns undefined if the id doesn’t exist.

Define the reference in the schema

This config has an authors data collection (one JSON file) and a blog collection whose posts point at an author and up to three related posts:

// src/content.config.ts
import { defineCollection, reference } from 'astro:content';
import { file, glob } from 'astro/loaders';
import { z } from 'astro/zod';

const authors = defineCollection({
  loader: file('src/data/authors.json'),
  schema: z.object({
    name: z.string(),
    role: z.string(),
    avatar: z.string().optional(),
  }),
});

const blog = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
  schema: z.object({
    title: z.string(),
    pubDate: z.coerce.date(),
    author: reference('authors'),
    related: z.array(reference('blog')).max(3).default([]),
  }),
});

export const collections = { authors, blog };

Import z from astro/zod; importing it from astro:content still works in Astro 7 but is deprecated. A collection can reference itself, which is what related does.

Write the frontmatter

The authors file is an array of objects with unique id fields:

[
  { "id": "komal", "name": "Komal Nakrani", "role": "Editor" },
  { "id": "sam", "name": "Sam Lee", "role": "Contributor" }
]

Posts then refer to entries by id. For glob() collections the id is the file path without the extension, so src/content/blog/astro-sitemap.md has the id astro-sitemap:

---
title: "Add a sitemap"
pubDate: 2026-11-02
author: komal
related:
  - astro-robots-txt
  - astro-canonical-urls
---

If a value isn’t a string (or a { collection, id } object pointing at the right collection), the build fails with a schema error naming the field. The frontmatter does not match schema fix explains how to read that message.

Resolve references in a page

---
// src/pages/blog/[id].astro
import { getCollection, getEntry, getEntries, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';

export async function getStaticPaths() {
  const posts = await getCollection('blog');
  return posts.map((post) => ({ params: { id: post.id }, props: { post } }));
}

const { post } = Astro.props;
const author = await getEntry(post.data.author);
const related = await getEntries(post.data.related);
const { Content } = await render(post);
---
<BaseLayout title={post.data.title}>
  <article>
    <h1>{post.data.title}</h1>
    {author && <p>By {author.data.name}, {author.data.role}</p>}
    <Content />
  </article>
  {related.length > 0 && (
    <aside>
      <h2>Related posts</h2>
      <ul>
        {related.map((r) => <li><a href={`/blog/${r.id}/`}>{r.data.title}</a></li>)}
      </ul>
    </aside>
  )}
</BaseLayout>

getEntry() accepts the reference object directly, so you don’t need to split out collection and id. The author && guard keeps the page rendering if someone deletes an author file before updating the posts that point at it.

Reverse lookups

References point one way. To list every post by an author on an author page, filter the posts collection by the reference id:

// src/pages/authors/[id].astro (frontmatter)
const posts = await getCollection('blog', ({ data }) => data.author.id === Astro.params.id);

The getCollection() filter tip covers more filter patterns. For the full schema API, see the content collections guide, our content collections docs, and the official reference() docs. If you’d rather start from a finished multi-author setup, the blog and editorial themes ship with author collections already wired.

FAQ

What does reference() return in Astro?

After validation, a reference field holds an object with collection and id. It doesn’t contain the target’s data; pass it to getEntry() to load the full entry.

Can an Astro collection reference itself?

Yes. z.array(reference('blog')) inside the blog schema is the usual way to model related posts or a series.

What happens if a referenced entry doesn’t exist?

The reference still validates as long as it’s a string, but getEntry() returns undefined for it. Guard the result in your template so a deleted author or post doesn’t break the page.