I'm trying to get the product collection containing only the products that appear in a search, from inside app\design\frontend[theme]\catalog\layer\view.phtml

I've tried:

$search = Mage::getSingleton('catalogsearch/advanced');
$searchCollection = $search->getProductCollection()->addAttributeToFilter('visibility', 4);


$searchCollection = Mage::getModel('catalog/category')->getProductCollection();
$searchCollection = Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection( $searchCollection );
$searchCollection = $searchCollection->getProductCollection();

Both of these return every single product on the site, not just the ones in the search results.

Can anyone help?

3 Answers 3


This was the only working solution I could find: https://stackoverflow.com/questions/20978893/whats-the-proper-way-to-call-catalogsearch-from-a-custom-block-in-magento

Though I did replace

$searchText = 'ipad';


$searchText = Mage::helper('catalogsearch')->getQueryText();

to make it dynamic.

  • if u got the answer. then close the question.
    – Helphin
    Commented Mar 18, 2014 at 8:33
  • This is a working solution, but it is bulky. I'm pretty sure there is a better solution out there.
    – Tyler V.
    Commented Mar 18, 2014 at 17:32

Well we know the product collection is loaded and present somewhere, so rather than querying the database again, if we know we are in the context of a search results page it is probably safe to just get the collection from the list block.

On a /catalogsearch/result page the list block will be at search_result_list, so we can fetch it from any block or template with:

$oListBlock = $this->getLayout()->getBlock('search_result_list')

And from here we can get the product collection off the block with:


Also the Mage_Catalog_Block_Product_List::getLoadedProductCollection() method calls Mage_Catalog_Block_Product_List::_getProductCollection which does some digging for a usable product collection, which is handy if you're writing a block that needs to be used in a few different circumstances.

  • worked great and seems the simplest way to accomplish this and the most D.R.Y friendly method
    – asherrard
    Commented Jun 27, 2017 at 21:40

Are you looking for this,

$search = Mage::getModel('catalog/product')->getCollection();
$searchCollection = $search->addAttributeToFilter('visibility', '3');
  • I don't think so, no. I am on the URL /catalogsearch/result/?q=example and trying to get the collection of products that matches the query "example" from inside app\design\frontend[theme]\catalog\layer\view.phtml so that I can make a list of categories that are used by the products returned in the search. I can't seem to get a collection that only contains the search results from this file. (Obviously app\design\frontend[theme]\catalog\product\list.phtml just uses $this->getLoadedProductCollection() but that's not available in layer.phtml)
    – Tyler V.
    Commented Mar 14, 2014 at 17:00

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.