• Fields

Content Relationship

This article explains what the content relationship field is and how to configure it.

Content writers can link documents through content relationship fields. Developers can fetch data from linked documents.

Content relationships are often used for managing categories, authors, and navigation menus.

Add a content relationship to a content model

  • Open Slice Machine

    In your Prismic project, start Slice Machine to begin editing content models.

    npx start-slicemachine --open
  • Add a content relationship field

    In Slice Machine, navigate to the slice, page type, or custom type you want to modify. Add a content relationship field.

    The label determines the label shown to content writers in the Page Builder. Use an easily understood name.

    The API ID determines the property name in the Document API. Use a short, snake-cased name.

  • (Optional) Allow a subset of document types

    Content relationships can be restricted to a subset of document types. For example, you may want to allow linking only to a Blog Post document.

    Open the field settings and select the allowed document types under the Types field.

Tips

  • Fetch content from linked documents

    The Prismic client’s graphQuery option fetches content from a linked document.

    const home = await client.getByUID("page", "home", {
      graphQuery: `
        {
          page {
            featured_blog_post {
              ...on blog_post {
                title
                description
              }
            }
          }
        }
      `,
    });
    
    // The linked blog post's title.
    const title = home.data.featured_blog_post.data.title;
  • Type fetched fields with TypeScript

    Fields fetched via graphQuery can be typed with TypeScript using a type assertion (the as operator).

    import { Content } from "@prismicio/client";
    
    const featuredBlogPost = home.data
      .featured_blog_post as typeof home.data.featured_blog_post & {
      data: Pick<Content.BlogPostDocument["data"], "title" | "description">;
    };
    
    const title = featuredBlogPost.data.title;
    //    ^ Typed as Content.BlogPostDocument['data']['title']
  • Filter queries by linked documents

    The Prismic client’s filters option can filter queries by a content relationship.

    This example fetches all blog posts from an author with the document ID YksUgRIAACEA-UZD:

    import { filter } from "@prismicio/client";
    
    await client.getAllByType("blog_post", {
      filters: [filter.at("my.blog_post.author", "YksUgRIAACEA-UZD")],
    });
  • Use isFilled.contentRelationship() to check if a content relationship has a value

    import { isFilled } from "@prismicio/client";
    
    if (isFilled.contentRelationship(slice.primary.featured_blog_post)) {
      // Do something if `featured_blog_post` has a value.
    }

Examples

Taxonomies

Nested menus

API response

Here is what a content relationship looks like from the Document API:

{
  "example_content_relationship": {
    "id": "XxnD3REAACYAk_CJ",
    "type": "page",
    "tags": ["music"],
    "slug": "vaporwave",
    "lang": "en",
    "uid": "vaporwave",
    "link_type": "Document",
    "isBroken": false
  }
}

When using the Prismic client’s graphQuery option, fields are added to the data property:

{
  "example_content_relationship": {
    "id": "XxnD3REAACYAk_CJ",
    "type": "page",
    "tags": ["music"],
    "slug": "vaporwave",
    "lang": "en",
    "uid": "vaporwave",
    "link_type": "Document",
    "isBroken": false,
    "data": {
      "title": "Vaporwave",
      "description": "Nostalgic, surreal, dreamy, and retro-futuristic."
    }
  }
}