to filter products by certain category following code sample is used


$collection = $this->productCollectionFactory->create();

return $collection;

This does not return products from sub categories even though all categories are marked "Is Anchor" => "Yes".

How can I retrieve products from given category and it's sub-categories based on "Is Anchor" functionality?

1 Answer 1


Figured it out. Turns out there is a method getAllChildren() that returns category ID along all sub-category IDs.

So to wrap it up:

$categoriesFilter = [
    'eq' => $category->getAllChildren(true)
  • Where do you get the $collection variable from? Apr 10, 2018 at 14:38
  • This works well but for completeness, and for other people reading this answer the $category object needs to be instantiated from the CategoryFactory to load your required category ID in order to pass the child array to the filter. See here for more info on loading $category magento.stackexchange.com/questions/96177/…
    – paj
    Oct 19, 2019 at 16:04
  • Some how it helps me today not same issue but your solution lead to me different solution that help me solve my problem Jan 4, 2021 at 12:34

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.