How to filter Magento products using 2 or more category filters?

By Dragan Simonovic

Fully working solution for displaying Magento products using 2 or more category filters.

I was searching for a way how to filter products in Magento using 2 or more categories and didn’t find any working solution. And this really frustrates me.

So I used example from Mukesh’s blog and started to read more about MySQL arrays and finally I succeed to filter Magento products using 2 or more category filters.

So here is the code:

$_productCollection = Mage::getModel('catalog/product')
 ->getCollection()
 ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('category_id', array(
 array('finset' => '45'),
 array('finset' => '49'))
 )
 ->addAttributeToSort('created_at', 'desc');

This solution works in Magento 1.7 and I didn’t test it for lower versions.

Hope this will help you to display products in Magento using multiple category filters.