There is an array of category ids i.e. array(18,19,25);

I want to get those products which has all these categories from above array. Product should present in 18,19 and 25.

Array values can be anything i.e. category ids.

  • You mean that you want to get an array of products which exist in categorys 18, 19 AND 25? Or do you want to get them if they are in any of 18, 19 and 25? Commented Nov 13, 2015 at 16:24
  • That product should present in 18,19 and 25.
    – ved
    Commented Nov 13, 2015 at 17:06
  • that means both 3 categories ? If product is in one category then what will be happen.
    – Amit Bera
    Commented Nov 14, 2015 at 12:36
  • 1
    Then that product should not be displayed. Product should present in all categories from the array
    – ved
    Commented Nov 14, 2015 at 12:47
  • You should extend little bit product collection. Check this answer Commented Jul 31, 2017 at 10:41

6 Answers 6


Let's Assume you have categoryIds Array

$categories = array(18,19,25);

First You have to do category join

    'category_id', 'catalog/category_product', 'category_id', 
    'product_id = entity_id', null, 'left'

Now add simple IN condition of collection

$pCollection->addAttributeToFilter('category_id', array('in' => implode(',', $categories)));


$productCollection = Mage::getModel('catalog/product')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => '18','19','25')));
  • please join with category product table.. you may be miss it
    – Amit Bera
    Commented Nov 13, 2015 at 18:01
  • Can you give me entire query.. @amit bera
    – ved
    Commented Nov 14, 2015 at 10:35
  • This produces an uncaught Mage_Eav_Exception: Invalid attribute name: category_id Commented Sep 27, 2018 at 12:43

This can be a bit scary but is working fine for me, with:

  • Magento 1.9
  • Flat Table
// An array of category ids
$categoryIds = [18,19,25];

// Get a collection of products
$collection = Mage::getModel('catalog/product')->getCollection();

// Add a filter for each category: only products belonging to each
// category will be in the collection
$i = 1;
foreach ($categoryIds as $categoyId) {

// Set distinct (avoid duplicate products)

Please try it

$catIds = array('10','82');
$productCollection = 
->joinField('category_id', 'catalog/category_product', 'category_id', 
'product_id=entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' =>  array('finset' =>
$catIds )))

$collection = Mage::getModel('catalog/product')
              ->addAttributeToSort('created_at', 'DESC')
   foreach ($collection as $product){
       echo $product->getId().'</br>';
       echo $product->getName().'</br>';
$products = Mage::getResourceModel('catalog/product_collection');
foreach($category_ids as $id)

UPDATE: this answer is wrong - it will filter the collection by the last category from $category_ids array - addCategoryFilter method doesn't support stacking. I didn't know that - what is the point of making a filter that supports only one category?

  • This loads products with an OR logic (products in a category or in a another category), but the question was about load product with an AND logic
    – Andrea
    Commented Apr 13, 2017 at 15:32
  • 1
    @Andrea we are both wrong. addCategoryFilter when applied multiple times takes into account only the last category. so my code will filter by the last category id in $category_ids. Commented Apr 13, 2017 at 17:02
  • Ah, ok, I didn't know it :) I found a way to do this by building a query with multiple joins. I added an answer.
    – Andrea
    Commented Apr 13, 2017 at 17:28

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.