1

I have several meta field like

Property Name----->bla bla, Property ID-------->1234, Property Address--->bla bla bla ,

and so on .

I want to print all the meta box name and their value . How can I do that ?

  • Is this what you mean: wordpress.stackexchange.com/questions/58834/… – kraftner Aug 11 '14 at 12:59
  • Please specify in your request if you mean to a specific post, post type or some sort of filter to narrow the list or if you wish to list all used meta fields to your site. – codearachnid Aug 11 '14 at 13:28
  • @codearachnid I meant a specifc post type . Like If I have a pos type "property" and I have several meta box for that like "Property Name","Property Address","Property Value" and so on . If I put the value of "Property Name" to "xyz" "Property Address" to "abc" "Property Value" to "1000" . I want to print name of meta box and their value like "Prperty Name"-->"abc" "Property Address"-->"xyz" "Property Value"--->1000 and so on . I can get only a specific meta box value by get_post_meta() function , but I want those info as I mentioned . – Mithun Sarker Shuvro Aug 11 '14 at 16:52
0

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.