Magento - Filter by attribute

Magento - Filter by attribute

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Filter by attribute

Filtering Products by Attribute in Magento

In Magento, filtering products based on an attribute value is a common requirement for category management or cross-selling purposes. In your case, you have defined an attribute cross_ref that contains a comma-separated list of values used to cross-reference products against each other.

Here’s how you can achieve this using the correct approach in Magento:

Understanding the Problem

The issue with your current code lies in the way Magento handles multi-value attributes. When you use the addFieldToFilter() method directly on the attribute name, it expects a single value match rather than matching within a comma-separated list.

Moreover, using addAttributeToSelect('*') is unnecessary when you’re specifically filtering by an attribute. It increases the query load and can slow down your application if dealing with a large number of products.

The Correct Approach

To filter products based on values in a multi-value attribute like cross_ref, you need to use the addAttributeToFilter() method with the correct condition:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('*'));
$collection->addAttributeToFilter(
    'cross_ref',
    array(
        'like' => '%1234%'
    )
);

foreach ($collection as $product) {
    var_dump($product->getData());
}

In this example, we first create a new product collection. We then use addAttributeToSelect() to select all attributes for each product in the collection.

The key change is in the addAttributeToFilter() method. Instead of filtering by an exact match ('1234'), we’re using a like condition with %1234%. This tells Magento to find products where the cross_ref attribute contains the value ‘1234’.

Explanation

The like operator in Magento uses SQL’s LIKE keyword under the hood. When you use % before and after the value, it becomes a wildcard search that matches any string containing the specified value.

For example, if a product has cross_ref set to “1234,5678,abcd”, using 'like' => '%1234%' will match this because ‘1234’ is contained within the comma-separated list.

Best Practices

Here are some best practices to follow when working with multi-value attributes in Magento:

  • Always use addAttributeToFilter() instead of addFieldToFilter() for multi-value attributes.
  • Use the like operator with % before and after the value for wildcard matching.
  • Avoid using addAttributeToSelect('*') unless necessary to reduce query load.
  • If dealing with a large number of products, consider implementing pagination or limiting results.

Remember that manipulating product collections directly can have performance implications. Always test your code thoroughly in a development environment before deploying it to a live store.

For more advanced filtering techniques and performance optimizations, check out our guide on Magento Performance Optimization at Laravel Company.

Conclusion

In this blog post, we’ve discussed how to filter Magento products based on values in a multi-value attribute using the correct approach. By understanding the difference between addFieldToFilter() and addAttributeToFilter(), and properly using the like operator with wildcards, you can effectively retrieve products that meet your criteria.

Always strive for clean, optimized code that minimizes query load to ensure your Magento store remains performant. Happy coding!

Catalog Database Performance Errors

Our network