1

I am looking to grab all of the custom post meta fields for a given post and display them on the page which i have accomplished with the following code.

However I would like to check if( !empty() ) the custom post meta values and not print any that are empty.

Other then that it should be pretty straight forward but I would appreciate the help.

<?php
$custom_fields = get_post_custom();
    foreach ( $custom_fields as $field_key => $field_values ) {
        if(!isset($field_values[0])) continue;
        if(in_array($field_key,array("hits","_wp_trash_meta_time","SKU","sub-category-2","sub-category-1","_wp_trash_meta_status","old_price","price","qty","additional_notes","buy_link","customlist1","customlist2","featured","image","images","_edit_lock","_edit_last","_thumbnail_id","_sexybookmarks_permaHash","_sexybookmarks_shortUrl"))) continue;
    foreach ( $field_values as $key => $value )                             
    echo '<li><strong>' . $field_key . ':</strong> ' . $value . '</li>';
}
?>

1 Answer 1

1

This can easily be done with default PHP functions.

// Loop through all fields, grab the content and assign it to a new array
foreach ( get_post_custom() as $custom )
    $new_custom[] = array_shift( $custom );

// Get rid of empty fields
$new_custom = array_filter( $new_custom );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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