• I have a lot of custom fields in my posts. Rather than using

    echo get_post_meta($post->ID, 'my_custom_field', true);

    for each custom field is it possible to create an array of all my custom fields and retrieve the custom field name and value for each one?

    I came across the following code which is close to what I need but it retrieves the key and id..

    `$custom_field_keys = get_post_custom_keys();
    foreach ( $custom_field_keys as $key => $value ) {
    $valuet = trim($value);
    if ( ‘_’ == $valuet{0} )
    continue;
    echo $key . ” => ” . $value . “<br />”;
    }`

Viewing 2 replies - 1 through 2 (of 2 total)
  • You’re on the right track. That code gives you all the ‘my_custom_field’ values for each custom field, so if you combine the two, you’ll can list all the values of all the custom fields for a post:

    $custom_field_keys = get_post_custom_keys();
    foreach ( $custom_field_keys as $key => $value ) {
    	$valuet = trim($value);
    
    	if ( '_' == $valuet{0} )
    		continue;
    		echo $value . ":<br />";
    		echo get_post_meta($post->ID, $value, true) . "<br/><br/>";
    }
    Thread Starter nemci7v

    (@nemci7v)

    Thanks! It works. For the first $value would it be possible to get the field name instead of id?

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

The topic ‘Get array of custom fields in post’ is closed to new replies.