I am adding a meta box in the create post/page interface and I want to get the ID of the post being edited/created so I can dynamically display the value of the input field.

From the WordPress codex :

/* Prints the box content */
function myplugin_inner_custom_box() {

  // Use nonce for verification
  wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );

  // The actual fields for data entry
  echo '<label for="myplugin_new_field">' . __("Description for this field", 'myplugin_textdomain' ) . '</label> ';
  echo '<input type="text" id= "myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />';

How do I pass the ID to myplugin_inner_custom_box()? So I can use the following within it:

// Get the value of the meta key that is associated to the page
  $as_meta_value = get_post_meta( $post_id, 'as_link_to_image', true );

and replace the whatever value with the value of the meta key in the input field.


Try get_the_ID() or global $post; $post->ID.

  • @Joann it isn't documented very well, but admin screens use many of same global variable that front-end does. – Rarst Jan 29 '11 at 20:21
  • 1
    Not sure if you saw my follow up, but $post is passed to the metabox callback function as a parameter so no need to get it from global space. – MikeSchinkel Jan 31 '11 at 1:24

Hi @Joann:

When creating post admin metaboxes, the standard way to get the Post's ID in a metabox is via a $post parameter in the callback function. The following is a subset of the code taken from the Codex found the same page you posted but after I updated it to remove 2 bugs:

Hopefully this is self-explanitory?

add_action('add_meta_boxes', 'myplugin_add_custom_box');
function add_my_meta_box() {
  add_meta_box('metabox_id', 'Metabox Title', 'my_metabox_callback', 
      'page', 'normal', 'low');
function my_metabox_callback($post, $metabox) {
  echo get_post_meta($post->ID,'my_custom_field',true); 
  • 1
    It's not easy to spot shoved under the $callback_args description(on the codex page linked to by Mike), but every metabox callback will receive the $post object as the first parameter, all you need do is include it in your callback function like in Mike's example, eg. function my_metabox_callback( $post ) – t31os Jan 30 '11 at 16:50
  • 1
    @Joann - Yes to what @t310s said; I realize now that my words my have sounded like a passive aggressive way to say "You should have seen it" but that was absolutely not my intent; it was more to say that there is a reference there in case you need it in the future. And if you had found it you would have had to deal with the fact that code had bugs you might not have know how to diagnose; I found and fixed those bugs because of answering this question for you. – MikeSchinkel Jan 31 '11 at 1:22
  • and t31os: Thanks! I tried imitating how the other functions from the sample code retrieve the post ID but it didn't work. Anyways, that's a cleaner way to do it indeed... – Joann Jan 31 '11 at 19:59
  • @Joann - Glad we could help. – MikeSchinkel Jan 31 '11 at 22:52

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.