List of addAttributeToFilter Conditionals In Magento

Filter collections in Magento or Magento 2 using addAttributeToFilter conditionals.

addFieldToFilter is adding WHERE condition in Mysql question to get the collection from the database, allowing to filter the collections like product collection, category collection, and many more by custom conditions. In a parallel manner, the integration of an advanced feature like the Multiselect Filter For Order Status in Magento 2 Order Grid takes this concept further.

Developing is all about if, when and else most of the time! (Developers, don’t get offended )

You may have to use conditions like equal, not equal, like, not like, in, not in, null, not null, greater than, less than, greater than equal to and less than equal to.

However, using the addAttributeToFilter conditionals in Magento is effective when used with correct syntax. So, here’s the list!

ConditionSyntaxExample
Equalseq$category->addAttributeToFilter('status', array('eq' => 1));
Not Equalsneq$category->addAttributeToFilter('name', array('neq' => 'test-category'));
Likelike$category->addAttributeToFilter('name', array('like' => 'UX%'));
Not Likenlike$category->addAttributeToFilter('name', array('nlike' => 'err-cat%'));
Inin$category->addAttributeToFilter('id', array('in' => array(1,4,98))));
Not Innin$category->addAttributeToFilter('id', array('nin' => array(1,4,98))));
NULLnull$category->addAttributeToFilter('description', array('null' => true));
Not NULLnotnull$category->addAttributeToFilter('description', array('notnull' => true));
Greater Thangt$category->addAttributeToFilter('id', array('gt' => 5));
Less Thanlt$category->addAttributeToFilter('id', array('lt' => 5));
Greater Than or Equals Togteq$category->addAttributeToFilter('id', array('gteq' => 5));
Less Than or Equals Tolteq$category->addAttributeToFilter('id', array('lteq' => 5));

That’s it.

Feel free to share the post with fellow developers on social media.

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...