It looks like you are trying to build a way to display a group of meta fields for your custom post type. Using get_post_custom
you can fetch all the custom post meta for a single post in 1 request. Then using a combination of looping you can build your output as you see fit. A roughed in example of how this can be accomplished is below:
wpse_157410_custom_meta_box( $display_fields = array(), $ID = null, $echo = true ) {
// if provided $ID is null fetch the current post
$ID = is_null( $ID ) ? get_the_ID() : $ID;
$output="";
$row_template = apply_filters( 'wpse_157410_custom_meta_box/row_template', '<div><label>%s</label><span>%</span></div>' );
$values_delimiter = apply_filter( 'wpse_157410_custom_meta_box/values_delimiter', ',' ):
$custom_post_meta = get_post_custom( $ID );
foreach( $custom_post_meta as $key => $values ){
$title="";
if( array_key_exists( $key, $display_fields ) || empty( $display_fields ) ){
$title = empty( $display_fields ) ? $key : $display_fields[ $key ];
}
// skip to the next record if we can't display a title
if( empty( $title ) )
continue;
$value = implode( $values_delimiter, $values );
$output .= apply_filters( 'wpse_157410_custom_meta_box/row_template',
sprintf( $row_template, $title, $value ),
$values,
$key,
$ID,
$custom_post_meta,
$display_fields
);
}
output = apply_filter( 'wpse_157410_custom_meta_box/output', $output, $custom_post_meta, $ID, $display_fields, $echo ):
if( $echo ) {
echo $output;
} else {
return $output;
}
}
To display this in your template you can just make a simple array of your field group. Decorate with the appropriate tags and then just drop the method into place and it’ll build the field output:
// get the meta group for the current post (for a specific post
// just supply the $post_id as the second argument)
$field_group = array(
'property_name' => 'Property Name',
'property_address' => 'Property Address',
'property_value' => 'Property Value'
);
wpse_157410_custom_meta_box( $field_group );