0

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

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

META BOX SCREEN SHOT

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?

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

1 Answer 1

1

Try this..

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

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

2
  • ishh,, that simple thing global $post... day saved.. Thanks a lot Abhik.. Sep 7, 2015 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, 2015 at 8:26

Your Answer

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

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