• Resolved combobreakerDOTcom

    (@combobreakerdotcom)


    I am adding a section to my theme’s post pages using add_meta_boxes. The value of the input is being saved to the postmeta table just fine when the page is initially saved or updated. However, after saving/updating the value of the text input just comes up blank.

    I tried to use:

    $desc = get_post_meta($post_id, 'sandbox_description', true);
    echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . $desc . '" size="30" />';

    to retive the value of the custom field and output it back to the value attribute. It seems that $desc never gets set this way. Is there something wrong with the way I am trying to retrieve the custom field? Is this the proper way to do this?

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    have you checked if $post_id is set to the post ID as you might expect?

    in functions.php, it might work using global $post; and $post->ID:

    global $post;
    $desc = get_post_meta($post->ID, 'sandbox_description', true);
    echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . $desc . '" size="30" />';
    Thread Starter combobreakerDOTcom

    (@combobreakerdotcom)

    Thank you! That worked perfectly.

    Would you be able to explain why in this instance I have to get $post_id that way, but when I save it I can do:

    update_post_meta($post_id, 'sandbox_description', $mydata);

    without a problem? I don’t quite understand the difference. I am using a minimally modified version of this example:

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_post_meta() in functions.php’ is closed to new replies.