Add filters using addAttributeToFilter Conditionals – Magento

Have you ever wanted to add a condition (where) in a Magento query?

Usually you'll see queries like this:

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

You can add conditions like this:

$_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('sku', array('like' => 'Example%'))->load();

Here's the list of conditional tags you can use in your queries:


$_products->addAttributeToFilter('status', array('eq' => 1));

Not Equals

$_products->addAttributeToFilter('sku', array('neq' => 'product-test-sku'));


$_products->addAttributeToFilter('sku', array('like' => 'UX%'));

Not Like

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));


$_products->addAttributeToFilter('id', array('in' => array(1,2,24)));

Not In

$_products->addAttributeToFilter('id', array('nin' => array(1,2,24)));


$_products->addAttributeToFilter('short_description', 'null');


$_products->addAttributeToFilter('short_description', 'notnull');

Greater Than

$_products->addAttributeToFilter('id', array('gt' => 10));

Less Than

$_products->addAttributeToFilter('id', array('lt' => 10));

Greater Than or Equals To

$_products->addAttributeToFilter('id', array('gteq' => 10));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

Check out the Magento Wiki page about Using Collections and modifiers.

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.