Magento 2.1 – How to Get Customer ID by Custom Attribute Value

customer-attributemagento-2.1

I need to get customer id before login by custom attribute value in Magento 2.1.5, for login by username.

$this->customerRepository->get($username);

Best Answer

Please Try below code :

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
    $collection = $customerObj->addAttributeToSelect('*')
                  ->addAttributeToFilter('your_attribute_name',$your_attribute_value)
                  ->load();
            
   $c_data=$collection->getData();
   $c_data[0]['entity_id'];
   echo $c_data[0]['entity_id'];