8

I want to write a function that returns users which have entered a certain value to a custom user field.

Lets say there is a custom birthday field for every user. What would be the best way to get all users with a certain birthday?

I could create a DB query that searches the field_data_field_birthday table and returns the entity_id(s) (which in this case would be the users id(s)) for which the field_birthday_value matches a given value and then load the user(s).

This seems a little bit crude, are there generic functions for this use case available? In the entity API or the field API maybe?

12

Simple example for the use case I asked about:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->fieldCondition('field_birthday', 'value', $birthday);

$result = $query->execute();
$users_ids = array_keys($result['user']);
  • Worked like a charm. – Anders Apr 20 '13 at 20:38
  • Please advise the namespace to include for this to avoid Fatal Error. – Pupil Jul 8 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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