Profile Fields Loop
The profile data loop is the most complex out of all the custom BuddyPress loops. It’s actually two loops in one, the first is to loop through profile field groups, and the second to loop through profile fields in that profile field group.
Standard Loop
<?php if ( bp_has_profile() ) : ?> <?php while ( bp_profile_groups() ) : bp_the_profile_group(); ?> <ul id="profile-groups"> <?php if ( bp_profile_group_has_fields() ) : ?> <li> <?php bp_the_profile_group_name() ?> <ul id="profile-group-fields"> <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?> <?php if ( bp_field_has_data() ) : ?> <li> <?php bp_the_profile_field_name() ?> <?php bp_the_profile_field_value() ?> </li> <?php endif; ?> <?php endwhile; ?> </ul> <li> <?php endif; ?> </ul> <?php endwhile; ?> <?php else: ?> <div id="message" class="info"> <p>This user does not have a profile.</p> </div> <?php endif;?>
Accepted Parameters
The bp_group_has_profile()
function will accept a number of parameters that will manipulate the data being returned.
-
profile_group_id optional
By default all groups and all fields will be displayed. If you provide the ID of a profile field group, then only the fields in this group will be displayed.
- Default value:
false
- Default value:
-
user_id optional
The ID of the user you want to fetch the profile data for. This is required if you are outside a member profile URL (/members/andy/…), otherwise it is the ID of the displayed user.
- Default value:
bp_displayed_user_id()
- Default value:
-
member_type optional
Limit fields by those restricted to a given member type, or array of member types. If
$user_id
is provided, the value of$member_type
will be overridden by the member types of the provided user. The special value of ‘any’ will return only those fields that are unrestricted by member type – i.e., those applicable to any type.- Default value:
false
- Default value:
-
hide_empty_groups optional
By default empty groups will not be displayed. If you provide the 0 value, then all the groups will be displayed.
- Default value:
true
- Default value:
-
hide_empty_fields optional
By default, only show empty fields if we’re on the Dashboard, or we’re on a user’s profile edit page, or this is a registration page. If you provide the 0 value, then all the fields will be displayed on your page.
- Default value:
!is_network_admin() && !is_admin() && !bp_is_user_profile_edit() && !bp_is_register_page()
- Default value:
-
fetch_fields optional
Whether to fetch each group’s fields.
- Default value:
false
- Default value:
-
fetch_fields_data optional
Whether to fetch data for each field. Requires a
$user_id
.- Default value:
false
- Default value:
-
exclude_groups optional
Comma-separated list or array of group IDs to exclude.
- Default value:
array()
- Default value:
-
exclude_fields optional
Comma-separated list or array of field IDs to exclude.
- Default value:
array()
- Default value:
-
update_meta_cache optional
Whether to pre-fetch xprofilemeta for all retrieved groups, fields, and data.
- Default value:
true
- Default value:
Advanced Usage
Fetch all the profile data for the user with ID 10
.
<?php if ( bp_group_has_profile( 'user_id=10' ) ) : ?>
Fetch the profile data for fields in the profile group ID 2
for the user with ID 10
.
<?php if ( bp_group_has_profile( 'user_id=10&profile_group_id=2' ) ) : ?>
Fetch the profile data for fields in the profile group ID 2
.
<?php if ( bp_has_profile('profile_group_id=2') ) : ?>
Fetch all the profile data, even empty fieds and groups, for current user.
<?php if ( bp_has_profile('hide_empty_groups=0&hide_empty_fields=0') ) : ?>
Fetch all the profile data for the user with ID 10
, excluding fields with ID 5, 6, 7
.
<?php if ( bp_group_has_profile( 'user_id=10&exclude_fields=5,6,7' ) ) : ?>