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();Code language: PHP (php)

You can add conditions like this:

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

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


$_products->addAttributeToFilter('status', array('eq' => 1));Code language: PHP (php)

Not Equals

$_products->addAttributeToFilter('sku', array('neq' => 'product-test-sku'));Code language: PHP (php)


$_products->addAttributeToFilter('sku', array('like' => 'UX%'));Code language: PHP (php)

Not Like

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));Code language: PHP (php)


$_products->addAttributeToFilter('id', array('in' => array(1,2,24)));Code language: PHP (php)

Not In

$_products->addAttributeToFilter('id', array('nin' => array(1,2,24)));Code language: PHP (php)


$_products->addAttributeToFilter('short_description', 'null');Code language: PHP (php)


$_products->addAttributeToFilter('short_description', 'notnull');Code language: PHP (php)

Greater Than

$_products->addAttributeToFilter('id', array('gt' => 10));Code language: PHP (php)

Less Than

$_products->addAttributeToFilter('id', array('lt' => 10));Code language: PHP (php)

Greater Than or Equals To

$_products->addAttributeToFilter('id', array('gteq' => 10));Code language: PHP (php)

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));Code language: PHP (php)

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

Leave a Reply

Your email address will not be published. Required fields are marked *