I have a product collection $collection which is created by Magento\Catalog\Model\ResourceModel\Product\Collection

code is:

$collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addAttributeToFilter('status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED));

How can I add multiple category filter in it?


First of all, do not use object manager, create your object from constructor

$protected $collection;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
    $this->collection = $collection;

For your category filter use this:

$categories = "12,13,14"; //Comma separated category IDs
$collection->addCategoriesFilter(array('in' => $categories));

Collection will join category table and will add your filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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