Magento - Add filter to getLoadedProductCollection()

Magento - Add filter to getLoadedProductCollection()

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Add filter to getLoadedProductCollection()

Magento: Filtering getLoadedProductCollection() Based on Custom Attribute

In Magento CE 1.9.0, you’ve created a custom product attribute called “custom_depth_check” and configured it to be used in the product listing for sorting. However, you’re facing an issue when trying to filter your loaded product collection based on this attribute using getLoadedProductCollection(). Let’s walk through this problem step-by-step.

The Problem with Your Current Approach

You’ve attempted to filter out products where custom_depth_check = 1 by clearing and reloading the collection, but it’s returning an empty set:

$_productCollection=$this->getLoadedProductCollection()
    ->clear()
    ->addAttributeToFilter('custom_depth_check', array('neq' => 1));

The issue lies in the order of operations. The load() method is actually called internally during the getLoadedProductCollection() call, so you don’t need to manually load it again.

A Step-by-Step Solution

To solve this problem, let’s break down the correct approach:

1. Start with a Fresh Collection

First, ensure you’re starting from a fresh collection that hasn’t been loaded yet:

$collection = Mage::getModel('catalog/product')->getCollection();

2. Apply Your Filters Before Loading

Next, apply your filters using addAttributeToFilter() before calling the load() method. This ensures the filter is applied at the correct stage of the collection’s life cycle:

$collection->addAttributeToFilter('custom_depth_check', array('neq' => 1));

3. Load the Collection

Finally, load the collection to retrieve the filtered products:

$products = $collection->load();

Putting It All Together

Here’s how you can filter out products with custom_depth_check = 1 in a single line of code:

$filteredProducts = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('custom_depth_check', array('neq' => 1))
    ->load();

Best Practices for Filtering Collections

Remember these best practices when working with Magento collections:

  • Always start with a fresh collection.
  • Apply your filters before loading the collection.
  • Use ->load() sparingly, as it can impact performance.

For more advanced techniques like filtering nested arrays or complex multi-level data structures, check out our article on Advanced Filtering in Magento.

Conclusion

Filtering loaded product collections based on custom attributes requires a careful approach. By following the order of operations we outlined—starting with a fresh collection, applying filters before loading, and then loading the data—the issue you encountered should be resolved.

Remember that understanding Magento’s internal processes is key to working effectively with its collections. Always ensure you’re filtering at the correct stage in the life cycle to achieve your desired results without unnecessary loads or performance hits.

Happy coding!

Magento 1 Catalog Configuration Orders

Our network