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>
	<?php foreach($songs as $song) {
		echo '<li>'.$song.'</li>';
	} ?>

Display custom field only if exists (logic)

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

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

Reference URL


  1. Daniel Groves
    Permalink to comment#

    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. Jeff Bridgforth
    Permalink to comment#

    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. Raphael Essoo-Snowdon
    Permalink to comment#

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

  4. JLee
    Permalink to comment#

    Thank you sooooo much! I adore your snippets!

  5. Slate Blank

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

  6. jaffa
    Permalink to comment#


    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. Mischell
    Permalink to comment#

    I solved my problem with Custom fields, thanks

  8. Matthew Taylor
    Permalink to comment#

    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?php endif; ?>
  9. Wayne Hatter
    Permalink to comment#

    Hey Chris,

    I love your code snippets sections. I was wondering…

    How did you style the blocks around the code that you present on your page?


  10. Michele
    Permalink to comment#

    Hi all, this works great but, let’s imagine I have 2 post with the same author:

    Track –>
    IS THIS IT by Author “The strokes”
    REPTILIA by Author “The strokes”

    when I put on Author “The Strokes on 2 (or more) different posts The cycle repeats author 2 (or more) times.

    Is there a Way to put something like this:

    The Strokes:
    ‘is this it’

    Michael Jackson
    ‘Beat it’

    And so on…

    is this possible?

    • Alexxali
      Permalink to comment#

      Could you please show us your code, if you have it? I’ll try to help if I can.

  11. yeBach
    Permalink to comment#

    I don’t know in what fantasy world this snippet would work @ChrisCoyer, but there is a SYNTAX error while outputing the reference url line.

        $url = get_post_meta($post->ID, 'snippet-reference-URL', true); 
        if ($url) {
            echo "<p><a href='$url'>Reference URL</a></p>";


        $url = get_post_meta($post->ID, 'snippet-reference-URL', true); 
        if ($url) {
            echo "<p><a href=' . $url . '>Reference URL</a></p>";
    • Yuvaraj
      Permalink to comment#

      ID, ‘snippet-reference-URL’, true);

      if ($url) {
      echo “Reference URL“;
      you are wrong because double quotes echo append like
      echo “Helllo”.$world.”welcome”;
      don’t use mixed double and single quotes for append

  12. Nick M
    Permalink to comment#

    Would anyone know how to extend the use of WP Custom Fields such as this:

    In my index.php (Blog Feed) I’d like a user with admin privlieges to be able to mark a post as FLAGGED (Turn color red) from the FRONT END. An ideal method would to have a bootstrap glyphicon button (flag) near the post title so that when clicked, the anchor link points to an action URL to update the post meta key ‘flag’ to ‘yes’.

    I plan to use if else statements to toggle this feature.

    My usage is for a VERY basic/simple site. AJAX is not required, I will just redirect the user back to the Front Page after they’ve clicked the action button.

    Let me know if I’m totally off with my approach.

  13. Jo / thedesertecho

    Thank you so much, my template files were leaving all sorts of random custom field text and with this I fixed it just to display what I wanted.

  14. Gal
    Permalink to comment#

    Thanks. Great solution for dealing with multiple Custom Fields. I have few wordpress sites managed by me and i seldom in search of the custom fields functionality…

  15. mark
    Permalink to comment#

    as simple as this is all i need.

    ID, ‘mood’, true); ?>


  16. philip
    Permalink to comment#


    I am so glad to find this site for helping in wordpress,
    So I have a big problem in the custom field which I lost the text format in it and it force me to write my content text inside the custom field in html.
    This problem appeared after I updated from wordpress 3.7.0 to wordpress 4.0 so please help me to fix this problem.
    If you want to see a photo for the problem I can send it to you in attachment.

    Best regards

    Thanks alot

    Philip V.

  17. Sarah Carney
    Permalink to comment#

    Exactly what I needed. <3

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.