Skip to main content
2 of 2
Improved Answer
Rizwan Khan
  • 2k
  • 2
  • 18
  • 41

You can use the Product Collection factory as below.

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory

$_collection = $this->_productCollectionFactory->create();
$_collection->addAttributeToSelect('*');
$_collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
// Your attribute code   
$_collection->addAttributeToFilter('size',  array('notnull' => true));

Get your product.

foreach ($_collection as $_product) {
    $_product->getName();
}
Rizwan Khan
  • 2k
  • 2
  • 18
  • 41