Magento filter product collection by multiple categories

Magento filter product collection by multiple categories

August 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento filter product collection by multiple categories

Filtering Magento Product Collections by Multiple Categories

In Magento, filtering product collections by multiple categories can be achieved using the addAttributeToFilter method applied to the category_ids attribute. However, as you’ve discovered, the syntax has evolved in recent versions.

The Old Way: No Longer Supported

Before version 1.4, it was possible to filter products across multiple categories using an array within the addCategoryFilter or addAttributeToFilter methods:

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('category_ids', array('finset' => array(1, 2)));

This would return all products that belong to either category ID 1 or 2. However, this method is no longer supported in Magento versions later than 1.4.

The New Way: Merging Collections

In the current version of Magento (1.6 and above), there isn’t a built-in way to filter by multiple categories directly. You’ll need to retrieve collections for each category separately and then merge them:

$categoryIds = array(1, 2);
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('status', '1');

foreach ($categoryIds as $id) {
    $categoryProductCollection = clone $productCollection;
    $categoryProductCollection->joinField(
        'category_id',
        'catalog/category_product',
        'category_id',
        'product_id=entity_id',
        null,
        'left'
    )->addAttributeToFilter('category_id', array('eq' => $id));
    $mergedCollection = Mage::getModel('core/resource_iterator')->cloneIterator($categoryProductCollection);
    foreach ($mergedCollection as $item) {
        $productCollection->addItem($item);
    }
}

This code first retrieves all product collections and then loops through each category ID. For each ID, it creates a cloned collection that joins the catalog/category_product table and filters by the specific category ID.

The resulting products from each category are then added to the main $productCollection, effectively merging them together.

Using Category IDs Directly

If you have a list of category IDs (e.g., from a customer’s wishlist or a third-party integration) and don’t need to retrieve additional data like name or image, you can filter directly using addAttributeToFilter:

$categoryIds = array(1, 2);
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*')
    ->addAttributeToFilter('status', '1')
    ->addFieldToFilter('category_ids', array('finset' => $categoryIds));

This will return all products that are part of any category in the $categoryIds array.

Conclusion

While Magento has evolved to require a more complex approach for filtering by multiple categories, the techniques outlined above provide effective solutions. The key is to understand how to clone and merge collections or use direct filtering based on your specific requirements.

Remember to always optimize your code by limiting the number of items returned when dealing with large product inventories. This can be done using setPageSize and setCurPage methods on the collection.

For more advanced Magento development tips, feel free to explore our guides at Laravel Company.

Catalog Pricing

Our network