get the products collection filter by attributes with array of values

get the products collection filter by attributes with array of values

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
get the products collection filter by attributes with array of values

Filtering Magento Products by Multiple Attribute Values

As an experienced developer, I often encounter requests from clients who want to retrieve a collection of products based on specific attribute values. In this case, you’re looking for a way to get all products whose color is either ‘red’, ‘blue’, or ‘green’—or whose brand matches any of the values in an array like [‘x’, ‘y’, ‘z’].

The key here is understanding that Magento uses EAV (Entity-Attribute-Value) architecture, which allows for great flexibility but also requires a slightly different approach compared to traditional relational databases. Let’s dive into how you can achieve this with your products collection.

Step 1: Load the Product Collection

First, we need to load our product collection using the Mage::getModel('catalog/product') function:

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

Step 2: Apply Attribute Filters

Now comes the crucial part—applying filters to your collection based on the attribute values you’re interested in. Since we want an “OR” relationship between our filter values, we’ll use the addAttributeToFilter method with the IN operator.

Here’s how you can do it for a color attribute:

$collection->addAttributeToFilter('color', array(
    'in' => array('red', 'blue', 'green')
));

And here’s how you would apply this same logic to a brand attribute:

$collection->addAttributeToFilter('brand', array(
    'in' => array('x', 'y', 'z')
));

Notice that we’re using the in operator, which means any product whose color or brand matches any of the values in our array will be included in the collection.

Step 3: Finalize Your Collection

With your filters applied, you can now work with this product collection as needed. You might want to add other filters, limit the number of results, or simply iterate through the products:

foreach ($collection as $product) {
    // Do something with each product...
}

Conclusion: Handling Complex Filters in Magento

The process I’ve outlined shows how to handle complex “OR” filters in your Magento product collection. By using the addAttributeToFilter method with the IN operator, you can create powerful and flexible queries that retrieve exactly the products you need.

Remember, while this example focuses on color and brand attributes, you can apply this same logic to any EAV attribute in your Magento setup.

For more advanced filtering techniques and best practices in Magento development, I highly recommend checking out the resources available at Laravel Company. They provide in-depth guides that cover everything from core functionality to custom extension development.

Catalog Database API CLI

Our network