✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Filtering products by sub-categories in Sanity.io schema

5 replies
Last updated: Sep 5, 2021
I have the following schema type.

export default {
  name: "products",
  type: "document",
  title: "Products",
  fields: [
{
      name: "categories",
      type: "reference",
      title: "Categories",
      to: {
        type: "category",
      },
    },

// other file

export default {
  name: "category",
  type: "document",
  title: "Product Categories",
  fields: [
    {
      name: "title",
      type: "string",
      title: "Title",
    },
  ],
};
I was wondering if in my deskStructure if it is possible to have a products folder -> then sub category folders filtered by the the categories that are selected for that product
Sep 5, 2021, 6:45 PM
I have tried something like this but it just gives me a list of all the products

S.listItem()
        .title("Products by category")
        .child(
          // List out the categories
          S.documentTypeList("category")
            .title("Product by page category")
            // When a category is selected, pass its id down to the next pane
            .child((categoryId) => {
              // load a new document list
              S.documentList()
                .title("Products")
                // Use a GROQ filter to get documents.
                // This filter checks for sampleProjects that has the
                // categoryId in its array of references
                .filter('_type == "products" && $categoryId in category[]._ref')
                .params({ categoryId });ddd
            })
        ),
Sep 5, 2021, 6:48 PM
I am getting closer but no cigar so far. This is products this
S.listItem()
        .title("Products by category")
        .child(
          // List out the categories
          S.documentTypeList("category")
            .title("Product by page category")
            // When a category is selected, pass its id down to the next pane
            .child((categoryId) =>
              // load a new document list
              S.documentList()
                .title("Products")
                // Use a GROQ filter to get documents.
                // This filter checks for sampleProjects that has the
                // categoryId in its array of references
                .filter(
                  '_type == "products" && $categoryId in categories[]._ref'
                )
                .params({ categoryId })
            )
        ),

Sep 5, 2021, 7:13 PM
Here is what a query of all the products looks like, but not sure how to filter by the categoryId
Sep 5, 2021, 7:22 PM
I tried something like this to no avail

*[_type == "products" && "87a1892e-506b-4055-b6a5-972de7692ee5" in categories._ref]
Sep 5, 2021, 7:24 PM
Yay!
This ended up working


                .filter('_type == "products" && references($categoryId)')
Sep 5, 2021, 7:27 PM

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?