I'm having trouble figuring out how to use the EntityQuery on custom field values. As an example I have a custom field under the User bundle that stores the country in which the user lives. There are three options: US, Canada and Mexico. I'd like to use the EntityQuery to directly query the 'field_country' to get a count of how many live in one country and get their uids. Here is an export from the database for this field:

enter image description here

The closest I've gotten is the code snippet below. This however is not working and I'm not sure where to go from here.

$results = \Drupal::entityQuery('user')
      ->condition('field_country', 'Canada')

Does anybody know? Does the entityquery give me the ability to look specific columns in a field? Thanks!

P.S. I know I could do this with a database query but I'd prefer to do it the Drupal way.

1 Answer 1


I think you might be missing a line:

$ids = \Drupal::entityQuery('user')
  ->condition('field_country', 'Canada')
  ->execute(); // This returns the user ids

// Load the users
$users = User::loadMultiple($ids);
foreach ($users as $uid => $user) {
    // $user contains the $user entity

More examples are located on the docs page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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