Going further to my previous post “Load Selected Categories In Magento“. we can get products from selected categories by add joins to query.
Here is code to Load products in magento from selected categories.
<?php // Pretend you got this array of category ids $categories = array(8,22); $collection = mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('status',1) ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left') ->addAttributeToFilter('category_id', array('in' => $categories)); $collection->getSelect()->group('e.entity_id'); ?&rt;
You can filter products from their attributes same as categories. This article links may help you for more information,
addAttributeToFilter conditions in magento
To know how to query directly to database like we did as joinField refer this article.
Direct SQL Queries In Magento
It is good practice and really important to query catalog only to those elements which we want. It affects when catalog becomes large. data will always increase while store get its pace. As developer we have to take care of performance. I hope this small piece of code helps someone.
Happy Coding With Magento !! 🙂