Magento – get products from multiple categories

category-productsmagento-1.9product-collection

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.

Best Answer

Let's Assume you have categoryIds Array

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

First You have to do category join

$collection->joinField(
    '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)));