0

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

1 Answer 1

1

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');
3
  • 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
    Commented Nov 1, 2011 at 16:26
  • 1
    Try this inside the foreach loop: $op .= $key . " => " . $value[0] . "<br />"; Commented Nov 1, 2011 at 16:40
  • Don't thank them, accept their solution if it worked. Commented Nov 2, 2011 at 7:54

Your Answer

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

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