I have products with a list of custom field values which are loaded via CSV, but not all products have a value for every custom field as the field is not relevant to that product. I want to be able to call up every relevant custom field name and its value on the page for each product.

I am using this which does display the custom field value (only) on the page:

<?php echo get_post_meta( get_the_ID(), 'current_promotions', true ); ?>

However I would like to display the field name and value for each product where appropriate.

Can anyone help?

1 Answer 1


You can use <?php the_meta(); ?> to do this (id you want to print them inside the Loop).

From Codex:

This is a simple built-in function for displaying custom fields for the current post, known as the "post-meta" (stored in the wp_postmeta table). It formats the data into an unordered list (see output below).

It must be used from within The Loop or in a theme file that handles data from a single post (e.g. single.php). the_meta() will ignore meta_keys (i.e. field names) that begin with an underscore.

So this:

<p>Meta information for this post:</p>
<?php the_meta(); ?>

Will output this:

<ul class='post-meta'>
    <li><span class='post-meta-key'>your_key:</span> your_value</li>
    ... other fields

Of course you can also do something like this (it's much easier to customize this code):

if ( $keys = get_post_custom_keys($post_id) ) {  // $post_id is ID of your post
    echo "<ul class='post-meta'>\n";
    foreach ( (array) $keys as $key ) {
        $keyt = trim($key);
        if ( is_protected_meta( $keyt, 'post' ) )
        $values = array_map('trim', get_post_custom_values($key));
        foreach ( $values as $k=>$value ) {
            if ( $value == '' ) {  // or whatever is your way to find if value is irrelevant
        $value = implode($values,', ');
        echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
    echo "</ul>\n";
  • Thank you for your help, but I'm afraid it has not answered my question. I'm looking for some code which will include all the custom fields but will only apply a field value to the relevant products. So for example if a product has a particular field value of 0 the custom field name will not be listed on the item's page - it will effectively be ignored. Can anyone help please?
    – Ajj
    Aug 1, 2013 at 13:28
  • Well, there are 2 solutions then. 1. You shouldn't add custom fields which won't apply to this product (You don't have to add all custom fields to all product - you can delete some custom fields from given product). 2. Just add an if to my second code (see updated answer). Aug 1, 2013 at 13:42
  • No problem. You can mark this answer as correct, if it helped you solve your problem. Aug 1, 2013 at 18:11

Your Answer

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

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