Code Snippet

Home » Code Snippets » WordPress » Using Custom Fields

Using Custom Fields

Dump out all custom fields as a list

<?php the_meta(); ?>

Display value of one specific custom field

<?php echo get_post_meta($post->ID, 'mood', true); ?>

"mood" would be ID value of custom field

Display multiple values of same custom field ID

<?php $songs = get_post_meta($post->ID, 'songs', false); ?>
<h3>This post inspired by:</h3>
<ul>
	<?php foreach($songs as $song) {
		echo '<li>'.$song.'</li>';
	} ?>
</ul>

Display custom field only if exists (logic)

<?php
    $url = get_post_meta($post->ID, 'snippet-reference-URL', true); 

	if ($url) {
	    echo "<p><a href='$url'>Reference URL</a></p>";
	}
?>

Reference URL

Subscribe to The Thread

  1. OMG. This is probably the most useful snippet I have ever come across, the amount of time I have spent in the past trying to remember this!

  2. Thanks. I was just looking for the “Display custom field only if exists (logic)” today for a project and could not find an example that I could understand how to change for my specific case. This snippet is just what I needed.

  3. The logic snippet worked wonderfully for me. Thanks.
    One question though, why haven’t you closed the tag?

  4. Thank you sooooo much! I adore your snippets!

  5. Very nice quick and easy reference for custom fields. Thanks a lot!

  6. Hi

    These snippets are great. I was wondering if you could help me.

    I am trying to match the title of a page to a custom field and display any posts that match that custom field.

    Total newbie here to both php and wordpress.

    Any help is much appreciated!

    :)

  7. I solved my problem with Custom fields, thanks

  8. I like to use the following chunk of code in my custom loops. It has a few advantages:
    I can attach ID’s to each custom key/value pair. I can reorder them any way i feel like it. If i need to change the key name for the custom field I can, and it wont break the old posts.. I can just add another chunk that pull up the new key. Any missing key/value pairs collapse to nothing. No broken layout! If I have a set the should be rendered together if the first value is present I can wrap them in opening and closing divs that only render if the first key exists.

    	
    
    			&lt?php if ( get_post_meta($post->ID, '01.0 Unit ID', true) ) : ?>
    				&ltdiv id="unitid" class="feature-pair xclearfix">
    					&ltdiv class="feature-label">Unit ID:&lt/div>
    					&ltdiv class="feature-value">
    					&lt?php echo get_post_meta($post->ID, '01.0 Unit ID', true) ?>
    					&lt/div>
    				&lt/div>
    			&lt?php endif; ?>
    
    

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~