• Hi there,

    Can someone help me 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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Old

    (@zilli)

    Thanks for your help.

    I created the follow shortcode and put it on functions.php, but it doesn’t return anything.

    //Applications list short-code
    
    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['my_custom_field'];
      foreach ( $my_custom_field as $key => $value )
        echo $key . " => " . $value . "<br />";
    }
    
    add_shortcode('applicant', 'jb_applicant');

    Any idea what could be?

    Thanks

    Couple of changes. Needed an opening brace on the foreach line and a closing one before the add_shortcode.

    Also, in order to show the output where the shortcode is placed, it needs to be returned, not echoed.

    Give this version a try:

    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['my_custom_field'];
       $op = '';
       foreach ( $my_custom_field as $key => $value ) {
          $op .= $key . " => " . $value . "<br />";
       }
       return $op;
    }
    add_shortcode('applicant', 'jb_applicant');
    Thread Starter Old

    (@zilli)

    Thanks, but it still returning nothing.

    Perhaps the post ID or the custom field name is not correct.

    Try adding some test text to the output. Instead of this:

    $op = '';

    use this:

    $op = '<h2>This is a test</h2>';

    Thread Starter Old

    (@zilli)

    I can see the test text.

    I’m sure that there are records on post_id 2171. Just to be sure, the get_post_custom( ), will fetches meta_key and meta_value’s data from that given post_id, right?

    That is correct, but this line:

    $my_custom_field = $custom_fields['my_custom_field'];

    selects only those meta_keys equal to ‘my_custom_field’.

    Is that what you want?

    Thread Starter Old

    (@zilli)

    Exactly.

    Well, I can’t explain it. The code worked for me on my test site, and the test text proves that it is being called.

    The only two other things that I can see that could cause the problem are the post_id and the meta_key value.

    Thread Starter Old

    (@zilli)

    Sorry to restart this thread, but now I can explain things better. So, let me try one more time.

    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);
       $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');

    If you want to print all meta_keys except selected ones, try this:

    function jb_applicant() {
       global $wpdb;
       $except = array( 'except-1', 'except-2' );
       $custom_fields = get_post_custom(2171);
       $op = '';
       foreach ( $custom_fields as $my_custom_field ) {
          foreach ( $my_custom_field as $key => $value ) {
             if (in_array($key,$except)) continue;
             $op .= $key . " => " . $value . "<br />";
          }
       }
       return $op;
    }
    add_shortcode('applicant', 'jb_applicant');

    Replace ‘except-1’ etc with keys to skip.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Help in creating a loop for post_id and meta_value tables’ is closed to new replies.