Reply To: Performance between loop and all conditional profile fields export
This code is just a start, I did not test to see if field groups will break it but you can get an example of spitting out fields as an object array variable on profile load and then using the variable to get the values instead of requesting each time.
put this in bp-custom.php
// creates global object array from profile fields
function bp_profile_fields_array() {
global $fields;
$fields = array();
if ( bp_has_profile() ) :
while ( bp_profile_groups() ) : bp_the_profile_group();
if ( bp_profile_group_has_fields() ) :
while ( bp_profile_fields() ) : bp_the_profile_field();
if ( bp_field_has_data() ) :
$field_name = bp_get_the_profile_field_name();
$field_value = bp_get_the_profile_field_value();
$field = array(
'field_name' => $field_name,
'field_value' => $field_value,
);
$fields[] = $field;
endif;
endwhile;
endif;
endwhile;
endif;
return $fields;
}
add_action( 'bp_before_member_header', 'bp_profile_fields_array' );
// echoes value of field from object array based on param
function bp_get_single_profile_field( $param ) {
global $fields;
foreach ( $fields as $key => $val ) {
if ( $val['field_name'] === $param ) {
echo $val['field_value'];
}
}
}
Then in your member templates:
<?php bp_get_single_profile_field('PROFILE FIELD NAME'); ?>