Magento: Combine eq and is null in addAttributeToFilter

Magento: Combine eq and is null in addAttributeToFilter

October 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Combine eq and is null in addAttributeToFilter

Combining Filter Conditions in Magento: “Is Null” and “Eq”

In Magento, applying multiple filter conditions to an attribute can be achieved using the addAttributeToFilter method on the collection object. However, combining specific conditions like “is null” and “eq” with array values presents a common challenge.

The Challenge

Let’s first look at the incorrect approach you’ve tried:

$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115, 116));

This code attempts to apply two separate filters:

  • The first filter matches rows where the attributename is NULL.
  • The second filter matches rows where the attributename equals either 115 or 116.

However, executing these lines as-is won’t produce the desired combined result. Magento processes each call to addAttributeToFilter independently, which leads to incorrect filtering behavior.

The Solution: Combining Filters

To achieve your goal of combining “is null” and “eq”, you need to pass both conditions in a single array when calling addAttributeToFilter. Here’s the corrected code:

$collection->addAttributeToFilter(
    'attributename',
    array('null' => true, 'in' => array(115, 116))
);

In this updated approach:

  • The 'null' => true condition matches rows where the attributename is NULL.
  • The 'in' => array(115, 116) condition matches rows where the attributename equals either 115 or 116.

By combining these conditions in a single call to addAttributeToFilter, Magento will correctly apply both filters simultaneously. This results in a collection that includes only the rows matching either of the specified conditions.

Best Practices for Filtering

When working with multiple filter conditions:

  • Always combine related conditions within a single call to addAttributeToFilter.
  • Use proper condition names, such as ‘null’ and ‘in’, to ensure correct behavior.
  • Be aware that addAttributeToFilter is additive by default; use addAttributeToFilter(array('or' => array())) if you need to create a group of OR conditions.

Conclusion

Combining filter conditions like “is null” and “eq” in Magento requires a proper understanding of the addAttributeToFilter method. By passing both conditions in a single array, you can achieve the desired filtering behavior efficiently.

Remember that careful structuring of your filter calls is essential for accurate results when working with multiple conditions. This approach will help optimize your product search and data manipulation processes within Magento.

For more advanced filtering techniques and best practices, feel free to explore our comprehensive guides on filtering in Magento at Laravel Company.

Magento

Our network