Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Creating a schema for a blog platform with sub posts in Sanity

10 replies
Last updated: Aug 8, 2022
Hello everyone 👋 I'm pretty new to Sanity and its schema architecture and I've been trying to find any solution regarding my query but couldn't.Basically, I'm trying to make a blog platform similar to
Buildspace where there are "posts" and within those posts, there are "sub posts". How would I go about creating a schema that makes this possible? My initial thought was to have a sub post array in the Post schema that references another schema of SubPost, but I also want to have control over the order of sub posts within a post, and would also like to structure sub posts nicely for scalability purposes.
What would be the most straightforward solution for this? I've attached a picture regarding the type of thing I'm trying to build. Any help would be immensely helpful!
Aug 7, 2022, 7:17 AM
Hey there, I’m doing something very similar with my ‘project’ doc schema, which has an array of ‘sections’, one of which is a ‘projectStructure’ object schema.Each of which has a ‘title’ field and an ‘aspects’ array field of type ‘projectStructureAspect’.
Each of which (in your case) could have whatever fields are relevant to you for lessons, like a ‘name’ string field and ‘text’ array of type ‘block’ (for a general rich text editor).
You can browse
my repo here to get an idea. Just pay attention to the ‘project’ schema and directory, copy/edit as necessary, and play with how Studio compiles it, doing a hard browser refresh sometimes to test changes.
Aug 7, 2022, 8:19 PM
Hello, thank you for the reply! I was actually thinking of something similar but since I need to control the order of the sub posts, I was wondering if an array type will retain the order of the subposts. Will I also be able to reorder the sub posts from sanity studio?
Aug 8, 2022, 5:30 AM
Yes, you can natively reorder items in an array.So it looks like you’d have 3 files:
• ‘project’ (document), fields: title, array of type:
• ‘step’ (object), fields: title, array of type:
• ‘lesson’ (object), fields: heading, subheading, body
Aug 8, 2022, 6:23 AM
Yes, I'm going to keep it simple for now and just have a "project" schema that has an array of type "lesson". Thanks a lot for the help, appreciate it :)
Aug 8, 2022, 6:47 AM
Sure! If individual lessons are ONLY relevant to a particular ‘project’ instance, then ‘lesson’ should be of type ‘object’. If a ‘lesson’ is reusable across projects, then of type ‘document’.
Aug 8, 2022, 6:53 AM
Yes one lesson is only relevant to a project but when I change the type of lesson to 'object' I'm only able to search for lessons, not create new ones. Any idea why?
Aug 8, 2022, 6:59 AM
On the contrary, if I set the type to 'document' I can create the lessons outside and refer to them in this list. But since a lesson is unique to a project it's not good to have documents of random lessons together right?
Aug 8, 2022, 7:02 AM
exactly, it’s the referencing part that’s making a lookup. Try this:
export default {name: "projectTopic",
  type: "object",
  title: "Project Topic",
  fields: [
    {
      name: "heading",
      type: "string",
      title: "Heading",
      options: {
        list: [...topics],
      },
      readOnly: ({ parent, value }) => {
        return (
          topics.map((t) => t.value).includes(value) &&
          (parent?.text || parent?.subtopic)
        );
      },
    },
    {
      name: "text",
      type: "array",
      title: "Text Area",
      of: [
        {
          type: "block",
          title: "Text Area",
        },
      ],
      hidden: ({ parent }) => {
        return parent?.heading === "Process" || !parent?.heading;
      },
    },
Aug 8, 2022, 7:04 AM
exactly, it’s the reference part that’s creating the lookup. Try this:
export default {
  name: "lesson",
  type: "object",
  title: "Lesson",
  fields: [
    {
      name: "title",
      type: "string",
      title: "Title",
    },
    {
      name: "text",
      type: "array",
      title: "Text Area",
      of: [
        {
          type: "block",
          title: "Text Area",
        },
      ],
    },
Aug 8, 2022, 7:10 AM
Oh gotcha! Removed the reference and it works flawlessly. Thanks a bunch :)
Aug 8, 2022, 7:45 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?