Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>';
}
?>
share|improve this question

1 Answer 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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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