Skip to:

How to get the value of a profile field of specific profile field group?

  • George Notaras



    I’m trying to figure out what is the most efficient way to get the value of a ‘profile field’ that belongs to a specific ‘profile field group’.

    Knowing the ‘field name’ and the ‘profile group name’ I’m thinking of the following:

    $field_value = bp_get_profile_field_data( array(
      'field' => $field_name,
      'user_id' => $user_id,
      'profile_group_id' => $group_id
    ) );

    Is there a standard BP API function that returns the ‘profile field group’ ID from the group’s name? I’m looking for something like xprofile_get_field_id_from_name() but for field groups.

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)

  • Brajesh Singh


    Hi George,
    No, There is no API function to fetch field group id from field group name and you will need to write your own sql query for the purpose.

    Also, In your example you have passed the profile_group_id to the bp_get_profile_field_data. Please note, it does not take that as a valid argument. It only accepts user_id and field. group id is not required for fetching field data.

    Now, If we forget about the group name API and come to the original requirement, Do you plan to fetch the data for multiple users(In loop) or just once/twice.

    For fetching data, these two functions work

    You can either use

    $data = xprofile_get_field_data( $field, $user_id = 0, $multi_format = 'array' )

    where $field is field id or name( If you know the id, It will fetch from any group, Name has the issue)

    or as you specified

    $data = bp_get_profile_field_data( array(
    	'user_id'	=> $user_id,
    	'field_id'	=> $field
    ) );

    The xprofile data APIs do not allow passing group in general. You either pass a field id or name.

    The problem will happen when multiple fields have same name. In that case, the first field with the name matches if you use the above functions.

    Another point, In both the cases, There is no group arguement and if you use field name, that will need one extra query(field id should be preferred if the id is known).

    Honestly speaking, It is ok to use it one or 2 time on page but avoid using it inside the loop. If you plan to use it inside the loop, you should try to first fetch the xprofile data and cache and then use any of these functions. One way to cache will be by calling

    BP_XProfile_ProfileData::get_value_byid( $field_id, $user_ids )

    Where $field_id is the id of field(not name) and $user_ids is one or more user ids.

    Hope that helps.

    George Notaras



    Hi Brajesh,

    Thank you very much for the insight. Your reply clarifies a lot of things for me. Really enlightening.

    I was planning to make my plugin able to understand the following field notation:

    some field name @ some field group

    for easier/friendlier association of xprofile fields to profile properties, for which the plugin can generate metadata.

    Now that I have this information, I’ll have to rethink about the implementation.

    Thanks again!

    Best Regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get the value of a profile field of specific profile field group?’ is closed to new replies.
Skip to toolbar