When I use addAttributeToFilter on Magento\Catalog\Model\ResourceModel\Product\CollectionFactory, with a custom attribute, I always get 0 results.

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;


$collection = $this->productCollection->create()
            ->addAttributeToFilter('custom_attribute',['lt' => 1]);

echo count($collection); //Returns 0

I also tried to use the search criteria object as Konrad Siamro sugested:

$search_criteria = $this->searchCriteriaBuilder
                ->addFilter('custom_attribute', 1, 'lt')
            $collection = $this->productRepository->getList($search_criteria);

echo $collection->getTotalCount(); //returns 0

But the results are the same. It works fine if I filter on a SKU, but my custom attribute returns nothing. I also tried adding the attribute to the flat product database and re indexing.

Update: Added code I used to create the attribute:

            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'frontend_class' => 'validate-digits',
            'label' => 'Custom Attribute',
            'input' => 'text',
            'class' => 'class',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
  • U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join – Ankit Shah Mar 7 '17 at 10:03

Use search criteria for filtering collections.
Go to http://alanstorm.com/magento_2_understanding_object_repositories/
and look for search criteria.

  • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.) – Dennis van Schaik Mar 7 '17 at 10:45
  • can You edit your post and show your code? :) – Konrad Siamro Mar 7 '17 at 10:55
  • I just edited my post. – Dennis van Schaik Mar 7 '17 at 11:23
  • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect? – Konrad Siamro Mar 7 '17 at 13:29
  • I added the code used to create the attribute as well. Can you check it for me? – Dennis van Schaik Mar 7 '17 at 14:57

Problem is that when you create your custom_attribute its still empty:

'user_defined' => false,
        'default' => '',
        'searchable' => false,

your default value is empty, but you check if value is lower than 1

->addFilter('custom_attribute', 1, 'lt')

I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to

->addFilter('custom_attribute', 1, 'eq')

and set the custom_attribue to 1 in one Product in Admin. Result is now one.

Problem is lower than one is not "empty"

Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.