I am showing Most popular products and I want to be able to exclude certain products from showing.

I thought the best way would be to exclude a category and when I want to exclude a product I can put it it the exclude category.

I need this to work even if the product is in a different category.

I am showing Most popular products with this:

$storeId2 = Mage::app()->getStore()->getId();
    $products2 = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect(array('name', 'price', 'small_image'))
        ->setOrder('ordered_qty', 'desc'); 


$x = 1;
    foreach($products2 as $prod2){
        $inventory2 = Mage::getModel('cataloginventory/stock_item')->loadByProduct($prod2);
        $actual_stock3 = (int)$inventory2->getQty();

    if ($actual_stock3<1){
    echo $prod2->getName() . ' - ' . $actual_stock3 . ' - ' . number_format($prod2->getData('ordered_qty'), 0) . '</br>';   
    if ($x++ == 10) break;  

How do I exclude all products in, lets say category 151 even if the product is in another category?

  • 2
    Instead of putting a product in a category in order to be excluded from the most popular, why not add a new product attribute called 'exclude_from_most_popular'. then you can filter by that.
    – Marius
    Commented Jul 27, 2016 at 9:24
  • Ah yes, seems simple now. Let me try it. Commented Jul 27, 2016 at 9:27

1 Answer 1


You can left join the catalog_category_product table to match products in your "exclude category", then filter your collection to only return products with no matches in the joined table:

This should work, given the "exclude" category as $excludeId:

    ['exclude_category' => 'catalog/category_product'],
    ['category_id' => $excludeId],
$products2->getSelect()->where(new Zend_Db_Expr('exclude_category.category_id IS NULL'));


I see, you are already joining the category index with addVisibleInCatalogFilterToCollection() which results in a duplicate column "category_id". I updated the code to use an alias for the joined table, exclude_category.

  • Can you show this in my code please, cant get it to work. I'm obviously doing it wrong :) Commented Jul 27, 2016 at 9:42
  • @MikeTimTurner See update. Add the code before $x=1 Commented Jul 27, 2016 at 9:53
  • btw, I tried it out and had to remove the addAttributeToSelect lines because in my case these attributes were not available in the reports collection. But YMMV Commented Jul 27, 2016 at 9:55
  • I didn't have to remove this, It worked great. thank you :) Commented Jul 27, 2016 at 10:15

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.