Skip to main content
code improvement
Source Link
user68116
user68116

Try this Code :

$productcollectionclass HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_objectManager>_productCollectionFactory->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productcollection        $collection->addAttributeToSelect('*');
        $collection->addFieldToFilter('my_attribute_code', array('in' => array(12,10)));
        return $collection;
    }
}

Try this Code :

$productcollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productcollection->addFieldToFilter('my_attribute_code', array('in' => array(12,10)));

Try this Code :

class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addFieldToFilter('my_attribute_code', array('in' => array(12,10)));
        return $collection;
    }
}
Source Link
user68116
user68116

Try this Code :

$productcollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productcollection->addFieldToFilter('my_attribute_code', array('in' => array(12,10)));