addAttributeToFilter Conditionals In Magento

addAttributeToFilter is a method that can be called on EAV collections in Magento. This includes product collections, category collections, customer collections and many more. In short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database, therefore allowing you to filter the collection by custom conditions. Mastering this function is key if you want to learn to write great code and great Magento extensions.

This article will focus on product collections in Magento but all of the addAttributeToFilter calls can be used on any EAV collection in Magento.

$_products = Mage::getResourceModel('catalog/product_collection')
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))

The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

addAttributeToFilter Conditionals

Notice above that I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.

Equals: eq

This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you're using.

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

Not Equals - neq

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

Like - like

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

One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.

Not Like - nlike

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

In - in

$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));

When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));

NULL - null

$_products->addAttributeToFilter('description', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('description', array('notnull' => true));

Greater Than - gt

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

Less Than - lt

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

Greater Than or Equals To- gteq

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

Less Than or Equals To - lteq

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


addAttributeToFilter only works with EAV entity types (products, categories, customers etc) in Magento. To use the same functionality with non-eav entity models, swap the addAttributeToFilter() method with addFieldToFilter(). This functions works in the exact same way and takes the same parameters, however it works on non-EAV entities.

Debugging The SQL Query

There are two ways to debug the query being executed when loading a collection in Magento.

// Method 1

// Method 2
$collection = Mage::getResourceModel('catalog/product_collection')

echo $collection->getSelect();

Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.

Read more about the Magento database in the Direct SQL Queries in Magento article.

28 thoughts on “addAttributeToFilter Conditionals In Magento”

  • Reps

    Thanks, great post!

  • Benham

    Really nice post and its nice to see you can use $collection->getSelect(); i always used to do $collection->printlogquery(true);

  • BT

    $collection->getSelect() is great for viewing the query, but it also provides a way to extensively customise your SQL query!

    Try the following on a product collection (or any collection) in Magento.

    $collection = Mage::getModel(‘catalog/product’)->getCollection();
    echo ‘


    This will list all of the available getSelect() functions and as you will be able to see, they are extremely useful!


    Thanx a lot great POST !!!!

  • NickSun

    NULL – null
    $_products->addAttributeToFilter('description', array('null'=>1));

    Not NULL – notnull
    $_products->addAttributeToFilter('description', array('notnull'=>1));

    see more at \lib\Varien\Data\Collection\Db.php

  • diszo

    Nice post! :)

  • Ela

    I want to show products based on my sorted list rather than default magento sort. So i added a attribute called product_display_order and will input the integer value while adding/updating depends on situations. So those things over but where and how can i add the condition to the product collection so that magento sorts the collection based on my attribute.

    Please help me

  • Kiran

    Simple post but useful for beginners....

  • roshan

    First of all thanks for this wonderful post. I have one question regarding collection or products that we get after filter.
    I am getting different collections using the custom attributes but pagination is not working for my custom collection. This is the url where I am using all the queries:

    I want to use pagination also when I choose custom collection. Any help or any hint? Thanks

  • Gary Hussey

    Excellent Post, thank you very much for sharing this knowledge, you've saved me hours of frustration and delay (shout out for Sir Topham Hat).

  • Thomas

    Very nice description!

    But can you tell me how to send this mysql command with the query: DATE(), because i want to only show the date part, without the time part, of certain attributes in the grid.
    this one: ->addAttributeToSelect('featured_from') gives: "2011-10-18 00:00:00", how do i adjust the query so it only returns "2011-10-18"?
    I'm thinking about something like ->addAttributeToSelect('featured_from', array('DATE')) ...

  • mahesh

    Good post and i have one problem

    How can i list the products sort by image in Magento?

    Ex: which product have the image that should come first and which product don't have the image that should come last

  • MagePsycho

    For NULL and NOT NULL, you need to use as:
    $collection->addFieldToFilter('field', array('null' => true)); // WHERE field IS NULL
    $collection->addFieldToFilter('field', array('notnull' => true)); // WHERE field IS NOT NULL

    Hope this helps.

  • Jakub Kramarz

    In reply to addFieldToFilter() vs addAtributeToFilter() problem:

    Function addFieldToFilter adds query condition to fields in specific table, on which you are running query. Unfortunatelty, products catalog uses EAV model, so there is no such field - it must be joined in special way to attributes table - it's why you must be using addAtributeToFilter.

    If specific collection is just instance of Mage_Core_Model_Mysql4_Collection_Abstract - you should be using addFieldToFilter. If it's Mage_Eav_Model_Entity_Collection_Abstract - addAtributeToFilter.

  • Mark Robinson
    Mark Robinson March 1, 2012 at 1:25 pm

    Thanks for the post. Any suggestions how to do something like this:
    ->addAttributeToFilter('special_price/price', array('lteq' => '0.8'))

    So I want the products that have been discounted by more than 20%

  • Debashis Chowdhury
    Debashis Chowdhury March 20, 2012 at 6:34 am


    I was roaming around for help and at came to this site. It helped me a lot on filter process.

    Thanks a lot for a nice article..

  • Mitch

    You can also get your current filters and apply them to the product collection.

    $_filters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->getActiveFilters();

    foreach ($_filters as $filter) {
    $filterData = $filter->getData();


    if ($key && $val) {
    $product_collection->addAttributeToFilter($key, array('in' => $val));

  • Roberto Alonso
    Roberto Alonso May 15, 2012 at 12:42 am

    Thanks for your post. Is there any way to add filter order by rating?

    thanks alot

  • beaudame

    Good article,
    I have one question if u can help
    How I can add filter to find data between two price range? for example show all products greater than 10 and less than 20 ?


  • aryan

    Hi beaudame,

    For this you have to add two filters something like below,

    ->addAttributeToFilter('price', array('gt' => $minprice))
    ->addAttributeToFilter('price', array('lt' => $maxprice))

  • clockworkgeek

    Roberto Alonso,

    I have a way to filter by ratings here:
    It's not as simple as "addAttributeToFilter" because ratings are not direct attributes of products. (I'm sorry for kidnapping thread for my own purposes)

  • rosh

    Great tutorial,
    A minor note filtering for NULL may require:
    $_products->addAttributeToFilter('description', array('null' => 1), 'left');
    (i.e add 'left' join as by default its 'inner' join which will not join to null attributes for an eav table as those rows would not exist on the attribute table)

  • Pradeep K Maurya

    how will we solve this issue "Configurable Product turn Out of Stock when all child product are out of stock"? on magento website


  • Leek

    I use filter : $_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
    But error : Fatal error: Call to a member function isStatic() on a non-object in E:\Xampp\htdocs\12tune\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php on line 430

    Help me use it. Thanks!

  • Christoph

    You are wrong with "$_products->addAttributeToFilter('description', 'null')"

    It must be "$_products->addAttributeToFilter('description', ['null' => true])"...
    Maybe you can update this!

Post your comment

FishPig Ltd