I have a custom post type and I created it using the following code.

/* DRAMAS */
register_post_type('dramas', array(
    'label' => 'Dramas',
    'show_ui' => true,
    'supports' => array('title', 'thumbnail', 'custom-fields', 'description'),
    'labels' => array (
        'name' => 'Dramas',
        'singular_name' => 'Drama',
        'menu_name' => 'Dramas'
) );

Following code for the meta box mark up

function custom_meta_box_markup() {
    $meta_box_text = get_post_meta( $post->ID, 'meta-box-text' );
            <label for="meta-box-text">Day</label>
            <input name="meta-box-text" type="text" value="<?php if ($meta_box_text) { echo $meta_box_text; }?>">
<?php } ?>

And my meta box code is:

function add_custom_meta_box()
    $id             = 'demo-meta-box';
    $title          = 'Schedule';
    $callback       = 'custom_meta_box_markup';
    $screen         = 'dramas';
    $context        = 'side';
    $priority       = 'high';
    $callback_args  = 'null';
    add_meta_box($id, $title, $callback, $screen, $context, $priority, $callback_args);

add_action("add_meta_boxes", "add_custom_meta_box");

Now I see the meta box like this:


I am able update the values and publish the post now. But when I add a some thing in the above metabox and publish it, it is displayed on the custom field right after my editor. NOT displayed on the (schedule) meta box (day) field.

custom filed

Why is this? how can I solve this?

  • Thy this on markup function $meta_box_text = get_post_meta( $post->ID, 'meta-box-text', true ); – Abhik Sep 7 '15 at 8:16
  • I tried already. NO luck... – Foolish Coder Sep 7 '15 at 8:19

Try this..

function custom_meta_box_markup() {
    global $post;
    $meta_box_text = get_post_meta( $post->ID, 'meta-box-text', true );
            <label for="meta-box-text">Day</label>
            <input name="meta-box-text" type="text" value="<?php echo $meta_box_text;?>">
<?php }

You are missing the $post global variable which holds the post data.

  • ishh,, that simple thing global $post... day saved.. Thanks a lot Abhik.. – Foolish Coder Sep 7 '15 at 8:24
  • @FoolishCoder yeah, that happens to the best of us. Please accept it as the answer if that solved your issue. – Abhik Sep 7 '15 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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