Title: Retrieve Custom Fields Individually
Last modified: August 19, 2016

---

# Retrieve Custom Fields Individually

 *  Resolved [ljmyers](https://wordpress.org/support/users/ljmyers/)
 * (@ljmyers)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/)
 * Hello,
 * Yes, I am yet another web monkey who knows just enough php to be dangerous and
   still learning about the powers of wordpress. FYI: I am using wordpress mu 2.8.4.
 * I am trying to retrieve and show custom fields in my posts but the thing is ...
   I want to retrieve them each individually and not all with one statement, allowing
   me more control over how they view in posts. I found this code in another topic
   and it works well but, I need to edit it so that I can use it once for each value
   that I want to retrieve instead of having it retrieve all values with one statement.
   Can anyone please help me?
 * [code]
    -  <?php
       $custom_fields = get_post_custom($post->ID); $custom_field_keys = 
      get_post_custom_keys(); foreach ( $custom_field_keys as $key => $name ) { 
      $valuet = trim($name); if ( '_' == $valuet{0} ){ continue; } if ($name != "
      ratings_users" && $name != "ratings_score" && $name != "ratings_average"){
      echo "
    - ";
       echo "**" . $name . ":** "; $my_custom_field = $custom_fields[$name]; 
      foreach ( $my_custom_field as $key => $value ) echo $value . "
    - ";
       } } ?>
 * [/code]
 * Thanks Bunches . . . Lana Banana

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Thread Starter [ljmyers](https://wordpress.org/support/users/ljmyers/)
 * (@ljmyers)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209461)
 * Polite Bump 🙂
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209463)
 * What about [get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)?
 * > This function returns the values of the custom fields with the specified key
   > from the specified post.
 *  Thread Starter [ljmyers](https://wordpress.org/support/users/ljmyers/)
 * (@ljmyers)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209473)
 * I tried that and it works great but it retrieves the keys and prints them in 
   the post as well as the values. I was wanting to retrieve only the values by 
   using individual php statements for each value. This way I could use tables, 
   etc. in designing the output. Thanks for the suggestion though.
 * I’ve searched and search for recourse code that I could get to work for me. I
   also tried the following code from the codex pages but I’m not knowledgeable 
   enough in php to get it to work so needles to say, it won’t. lol
 * [code]<?php
 *  $note_values = get_post_custom_values('note');
    foreach ( $note_values as $key
   => $value ) { echo "$key => $value ('note')"; }
 * ?>[/code]
 * I get this as a result . . .
 * [code]Warning: Invalid argument supplied for foreach() in /home/ljmyers/domains/
   reciperenaissance.com/public_html/wp-content/themes/citrus islandwp/single.php
   on line 11[/code]
 * This is for multiple values per key anyway and I only have one value per key.
 *  [Mark Ratledge](https://wordpress.org/support/users/songdogtech/)
 * (@songdogtech)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209486)
 * Maybe a new WP query? I use this to print the contents of the custom field “test”
   from the latest post in the category “posts”. You need to be able to run PHP 
   in posts using a plugin.
 * `<?php $my_query = new WP_Query('category_name=posts&showposts=1'); while ($my_query-
   >have_posts()): $my_query->the_post(); ; echo get_post_meta($my_query->post->
   ID, "test", $single = true); ?><?php endwhile; endif; ?>`
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209550)
 * Hmm, get_post_meta works for me in the loop in the WordPress Default theme’s 
   index.php:
 *     ```
       <?php
       $value = get_post_meta($post->ID, 'cf1', true);
       echo 'this is value of cf1 key '.$value;
       ?>
       ```
   
 *  [Mark Ratledge](https://wordpress.org/support/users/songdogtech/)
 * (@songdogtech)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209584)
 * You’re right, that will work. No real need for a new query. I should have mentioned
   the reason behind me using new queries – I use a bunch of them on a landing page
   for generating links to the most recent published posts in different categories
   and previewing upcoming posts via custom fields.
 *  Thread Starter [ljmyers](https://wordpress.org/support/users/ljmyers/)
 * (@ljmyers)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209755)
 * Thanks for the help guys and my apologies for the delayed response. We spent 
   the weekend at the farm.
 * I have custom fields named(key) **note**, **description** and so on. Each has
   a value that will change with each post. How would I edit the following code 
   to retrieve and print individually in my posts? If it helps, it is a recipe site.
   I know . . . “Not another one” LOL
 *     ```
       <?php
       $value = get_post_meta($post->ID, 'note', true);
       echo 'this is value of cf1 key '.$value;
       ?>
       <?php
       $value = get_post_meta($post->ID, 'description', true);
       echo 'this is value of cf1 key '.$value;
       ?>
       ```
   
 * My apologies for my ignorance as well. I realize that I have much to learn about
   php.
 *  [thenameisnick](https://wordpress.org/support/users/thenameisnick/)
 * (@thenameisnick)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209757)
 * I would recommend something like this at the very top of your template file:
 *     ```
       <?php
       $note = get_post_meta($post->ID, 'note', true);
       $description = get_post_meta($post->ID, 'description', true);
       ?>
       ```
   
 * With that, you can then echo out $note and $description interchangeably anywhere
   you want on that page. Should be very simple, so if you are still having problems,
   let me know.
 *  Thread Starter [ljmyers](https://wordpress.org/support/users/ljmyers/)
 * (@ljmyers)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209759)
 * I finally found something that works exactly how I want it to and thought I would
   post it here for anyone wanting to accomplish the same thing . . .
 *     ```
       <?php if(get_post_meta($post->ID, "note", true) || get_post_meta($post->ID, "description", true)): ?>
   
       <div style='margin-bottom:10px;'>
       <?php if(get_post_meta($post->ID, "note", true)): ?>
       <?php $key="note"; echo get_post_meta($post->ID, $key, true); ?>
       </div>
       <?php endif; ?>
   
       <div style='margin-bottom:10px;'>
       <?php if(get_post_meta($post->ID, "description", true)): ?>
       <?php $key="description"; echo get_post_meta($post->ID, $key, true); ?>
       <?php endif; ?>
       </div>
   
       <?php endif; ?>
       ```
   
 * Thanks for the time spent helping me. 🙂
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209768)
 * Good to see that the three recommendations to use get_post_meta was followed.
   Thanks for the feedback. Marking resolved.
 * Also see you marked this thread with the MU tag so should point out there is 
   also an MU forum at [http://mu.wordpress.org/forums/](http://mu.wordpress.org/forums/)

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Retrieve Custom Fields Individually’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [Individually](https://wordpress.org/support/topic-tag/individually/)
 * [retrieve](https://wordpress.org/support/topic-tag/retrieve/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 4 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/retrieve-custom-fields-individually/#post-1209768)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
