tags:

views:

29

answers:

1

I try to get productsCollection by this code:

    $collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('is_anchor')
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter(array(4,5))//$_categories)
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite()
    ->load();

$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer             = Mage::getSingleton('catalog/layer');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
foreach($productCollection as $product){
    print_r($product->getCategoryIds());
}

But line addIdFilter(array(4,5)) not work, and i see all product, even that no in some categorys.

What is wrong?

A: 

try passing a string with comma separated ids:

->addIdFilter("4,5")

If that does not work you can always try:

->addAttributeToFilter(’id’, array('in' => array(4,5))) 
silvo