Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a loop to read all the meta_value records from a given post_id. The idea is to gather all the meta_value's data from wp_postmeta table related to post_id number and print them as a list. But so far, I couldn't make this work. The issue is my 'custom_fields' variable. There I need to put my meta_key. But I have several meta_key per post_id. For example, the post_id 2171 has data2,date3,data4,data5.. data19 as meta_key. So, how can I get this working?

function jb_applicant() {
   global $wpdb;
   $custom_fields = get_post_custom(2171); //2171 is the post_id that I'm trying to gather the data from
   $my_custom_field = $custom_fields['data2'];
   $op = '';
   foreach ( $my_custom_field as $key => $value ) {
      $op .= $key . " => " . $value . "<br />";
   }
   return $op;
}
add_shortcode('applicant', 'jb_applicant');

Thanks

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You're close, but not quite. Try it like this:

function jb_applicant() {
   $custom_fields = get_post_custom(2171);
   $op = '';
   foreach ( $custom_fields as $key => $value ) {
      $op .= $key . " => " . $value . "<br />";
   }
   return $op;
}
add_shortcode('applicant', 'jb_applicant');
share|improve this answer
    
Almost there, but not yet. The code gave me all the meta_key and the word 'array'. Like below: data2 => Array data3 => Array data4 => Array data6 => Array data7 => Array data9 => Array data11 => Array data13 => Array data14 => Array data15 => Array data16 => Array data18 => Array data19 => Array But what I need is the record from meta_value. –  Daniel Nov 1 '11 at 16:26
1  
Try this inside the foreach loop: $op .= $key . " => " . $value[0] . "<br />"; –  chrisguitarguy Nov 1 '11 at 16:40
    
Thank you guys! –  Daniel Nov 1 '11 at 16:59
    
Don't thank them, accept their solution if it worked. –  EarnestoDev Nov 2 '11 at 7:54
add comment

Your Answer

 
discard

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.