0

I have a products page on which I'm filtering with Sprig. There are some categories in the form of checkboxes. These categories have some subcategories. The categories are a structured section with nested categories.

Example:

  • Category 1
    • Subcategory a
    • Subcategory b
  • Category 2
    • Subcategory c
    • Subcategory d

What I currently got working is to show the level 2 categories filters based on the chosen main category. What does not work is doing the same but for 2 main categories at the same time.

When the user clicks Category 1 I want to a query to return Subcategory a and Subcategory b.

When the user clicks Category 1 AND Category 2 I want to a query to return Subcategory a, Subcategory b, Subcategory c and Subcategory d.

My query:

{% set typeOptions = craft.entries()
 .section('categories')
 .orderBy('title ASC')
 .descendantOf(subcategories|join(','))
 .descendantDist(1)
%}

The reason why I am using join is because descendantOf does not accept an array and it seems it also does not accept comma separated string. It does accept a non comma separated string. I'm guessing it converts it to an INT internally.

How do I fix this?

3
  • Can you let us know what subcategories is defined as in your codebase? Seeing how you defined it will help us know what data type it is and whether that's a factor
    – cherrykoda
    Sep 22 at 18:25
  • It's all just a big structured section. And I'm trying to get the title field.
    – Matthias
    Sep 27 at 9:22
  • Right, but how are you calling it? Depending how it could be an object, an ID value...
    – cherrykoda
    Oct 1 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.