I have Profile2 field name uid_intranet which is unique. And in one of my module, I want to get $user object by uid_intranet. The API is kinda like:

$users = profile2_get_users('uid_intranet', 100)

But no such API. So any solutions?


UPDATE 01, my complete code:

// Helper
function _mymodule_get_profiles_by_profile2field($field_name, $field_value) {
  $table = "field_data_{$field_name}";
  $column = "{$field_name}_value";

  // Profile IDs
  $pids = db_select($table, 'ui')
    ->fields('ui', array('entity_id'))
    ->condition($column, $field_value, '=')
  if (is_array($pids) && count($pids) > 0) {
    $profiles = profile2_load_multiple($pids);
  return $profiles;

2 Answers 2


Why not adding your helper function to query the database, something like below

function mydmoulename_get_user_by_prfoile2field($field_value) {
    $user = new stdClass();
    $user_id = db_select('field_data_field_uid_intranet', 'ui')
        ->fields('ui', array('entity_id'))
        ->condition('field_uid_intranet_value', $field_value, '=')
    if (is_array($user_id) && count($user_id) > 0) {
        $user = user_load_multiple($user_id);
    return $user;


When you add a field to profile, a custom table is created in database,so check your database and get the table name, in above example I am using table name as "field_data_field_uid_intranet". When you browse the table you will find column where it wil be storing the field value against the user who added the fied, you need to check the column name which is holding the field value in above exampe I am using "field_uid_intranet_value" as column name change it according to your requirement.

Once done, now you have your own function simply call it passing the field value as argument like

$user = mymodulename_get_user_by_prfoile2field(100);

you will get the complete user object as return value.

  • Given the fact we don't know how many users will meet condition, it would be probably a bit better to make this function plural and use user_load_multiple. Other than that, it seems pretty OK, of course only if there really is no such (hidden) functionality in the Profile2 API.
    – Mołot
    Commented Nov 19, 2013 at 11:41
  • @Mołot it seems the field is having one to one mapping only since field being unique so that's why we are using user_load().
    – arpitr
    Commented Nov 19, 2013 at 11:45
  • Well, I just hope no future user with non-unique field will get caught by surprise simply copy-pasting your code :P And since most functions get_x_by_y returns sets (exception for get_x_by_ID), it would be simply the sake of code consistency for me to keep new ones that way even if they don't have to, strictly speaking. But oh well, that's just my opinion :)
    – Mołot
    Commented Nov 19, 2013 at 11:52
  • I just update the post, adding my solution based on this answer. Thanks!
    – Bonn
    Commented Nov 22, 2013 at 2:57

The above answer is quite wrong, the entity_id is not the user id its the profile ID. You must join the above table with profile in order to get the user id.

for instance

$result = db_query('
SELECT p.uid FROM {field_data_field_fb_id} fb join {profile} p
on p.pid = fb.entity_id
WHERE fb.field_fb_id_value = :fb_id', array(':fb_id' => $user_fb_id));

$user_id = $result->fetchCol();

The entity_id is just a reference to the profile table.

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.