1

I have a component which lists events and can be configured to filter by category.

Query:

query Events($filter: [String]) {
  entries(
    section: "event"
    relatedToCategories: {group: "group", slug: $filter}
  ) {
    __typename
  }
}

Variables:

{
    "filter": null
}

The filtering is working fine in most cases except with events that don't have a group category. I haven't found any value for $filter that fetches those. Setting it to an empty array or null only gets the events that have at least one category.

What's the correct approach?

Update

I also tried passing the entire filter object to the query:

Query:

query Events($filter: [CategoryCriteriaInput]) {
  entries(
    section: "event"
    relatedToCategories: $filter
  ) {
    __typename
  }
}

Variables:

{
    "filter": [{group: "group", slug: "some-slug"}]
}

This works when a filter is set but not without one.

Passing null or [] throws this error: Calling unknown method: craft\\elements\\db\\EntryQuery::relatedToCategories() Passing [{}] again only returns the entries which have any category set.

1 Answer 1

1

I figured it out. The second option (sending the whole filter as a variable) but instead of passing null or [] simply omitting it (or setting it to undefined) worked. Not sure why null and undefined act differently though.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.