You can use the customer repository located at app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php
(having API \Magento\Customer\Api\CustomerRepositoryInterface
) like:
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var \Magento\Framework\Api\SearchCriteriaInterface
*/
protected $searchCriteria;
/**
* @var \Magento\Framework\Api\Search\FilterGroup
*/
protected $filterGroup;
/**
* @var \Magento\Framework\Api\FilterBuilder
*/
protected $filterBuilder;
/**
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
* @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
*/
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Framework\Api\SearchCriteriaInterface $criteria,
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
\Magento\Framework\Api\FilterBuilder $filterBuilder
) {
$this->customerRepository = $customerRepository;
$this->searchCriteria = $criteria;
$this->filterGroup = $filterGroup;
$this->filterBuilder = $filterBuilder;
$customers = $this->searchCustomersByAttributeValue('my_attribute', '3aF');
}
public function searchCustomersByAttributeValue($attributeCode, $value)
{
$this->filterGroup->setFilters(
[
$this->filterBuilder
->setField($attributeCode)
->setConditionType('eq')
->setValue($value)
->create()
]
);
$this->searchCriteria->setFilterGroups([$this->filterGroup]);
$customersList = $this->customerRepository->getList($this->searchCriteria);
$customers = $customersList->getItems();
return $customers;
}
As a result you have the list of the matched customers:
Here (in dev docs) you can find some other filter types.