WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

$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

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


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.