Magento addFieldToFilter: Two fields, match as OR, not AND

Magento addFieldToFilter: Two fields, match as OR, not AND

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Magento addFieldToFilter: Two fields, match as OR, not AND

You want to filter a collection by two or more fields where either condition can be true, but you're running into an error when using addFieldToFilter with multiple flat arrays. In standard Magento 1 code, the common pattern shown in older tutorials looks like this:

$collection->addFieldToFilter(array(
    array('name' => 'orig_price', 'eq' => 'Widget A'),
    array('name' => 'orig_price', 'eq' => 'Widget B'),           
));

At first glance, this seems to express "match if the name is Widget A OR Widget B." However, Magento's internal implementation interprets multiple filter entries differently than you might expect, and the result can break depending on how the conditions are structured.

The error you're seeing—Warning: Illegal offset type in isset or empty followed by Invalid argument supplied for foreach()—comes from how Magento parses the filter array inside its collection logic. The issue isn't with your intent; it's with the shape of the data passed to addFieldToFilter.

How Magento Parses Filter Conditions

Magento's Db_Collection class expects a specific structure when handling multiple conditions. When you pass an array of arrays where each inner array defines a condition, Magento treats them as separate filters that must all be satisfied unless grouped differently. This leads to logical AND behavior instead of the intended OR logic if not handled correctly.

In your original working example:

addFieldToFilter(array(
    array('remote_ip', array('eq' => '127.0.0.1')),
    array('customer_firstname', array('eq' => 'gabe'))),
    array('eq' => array(1, 2, 3))
);

You're actually passing a single field (remote_ip or customer_firstname) with multiple values via the third parameter, which Magento understands as an OR condition across those values. That's why it works without hacking core files.

Why the Flat Array Approach Fails

The failing pattern repeats the field name twice with different values:

array(
    array('name' => 'orig_price', 'eq' => 'Widget A'),
    array('name' => 'orig_price', 'eq' => 'Widget B')
)

This structure causes Magento to attempt processing each condition independently but without proper grouping. Internally, the code tries to iterate over what it expects to be a single condition object or a value list, leading to type mismatches and the errors you're seeing in lib/Varien/Data/Collection/Db.php.

The key is that Magento does not interpret multiple identical field names with different conditions as an OR clause unless they are wrapped inside a nested structure that explicitly defines logical grouping.

The Correct Pattern for OR Logic

To achieve OR logic across multiple values of the same field, pass the values directly to the filter method:

$collection->addFieldToFilter('orig_price', array('in' => array('Widget A', 'Widget B')));

This is the cleanest and most maintainable approach. Magento automatically generates an OR condition when using the in operator, which avoids modifying core files entirely.

If you need to filter by different fields where matching any one of them satisfies the condition, combine multiple calls:

$collection->addFieldToFilter('orig_price', array('eq' => 'Widget A'))
    ->addFieldToFilter('orig_price', array('eq' => 'Widget B'));

However, chaining addFieldToFilter like this results in AND logic by default because each call adds a new condition to the existing filter group. To express OR across different fields, you must use the nested structure Magento supports for grouping conditions.

Using Nested Conditions Properly

To implement OR logic between different fields without modifying core files, wrap conditions inside a single array and specify the logical operator:

$collection->addFieldToFilter(
    array(
        'conditions' => array(
            array('orig_price' => array('eq' => 'Widget A')),
            array('orig_price' => array('eq' => 'Widget B'))
        ),
        'condition_type' => 'or'
    )
);

This structure tells Magento to treat the inner conditions as a group connected by OR. Note that this syntax may vary slightly depending on the exact version of Magento you're running, so always verify behavior in your environment before relying on it in production code.

Avoiding Core Modifications

The temptation to patch lib/Varien/Data/Collection/Db.php is understandable, but core modifications make upgrades painful and introduce subtle bugs. Whenever possible, prefer built-in operators like in, is_not_null, or structured nested conditions that align with Magento's expected input format.

If you're working on a custom module or theme, encapsulate these patterns in reusable helper classes to keep your logic clean and version-safe. This approach ensures compatibility across patches and upgrades while keeping your codebase maintainable.

Our network