-1

how to arrange blog post by categories in next-sanity.

click to view what I meant to arrange

All of my blogs in one line like category 1

category 1 blog, category 2 blog, category 3 blog

i want them like category 1

category 1 blogs.

category 2

category 2blogs.

Mohid Naeem
  • 41
  • 1
  • 5

1 Answers1

0

If you want to group posts by category, you can do that in your query by using joins and the special references variable:

*[_type == 'category'] {
  ...,
  "posts": *[_type == 'post' && references(^._id)]
}

Here we are querying the category documents first *[_type == 'category'] then for each category projection we are querying for all post documents that references this category by id: *[_type == 'post' && references(^._id)]. The ^ parent operator refers the parent document, in this case the category document.

For more details you can check this link from the official docs. I hope this helps

Eytyy
  • 88
  • 8