0

The add_meta_box() function needs a callback function as an argument to display the meta box on the edit page of a custom type post. My problem is I can not display anything, say in the input type=text (for example the values of an already posted post), because I don't have the ID of the post...

get_post_meta needs the ID of the post to display the values!

How can I get the ID of the post?

0

Let’s say your post type is named product. You register your metabox with …

add_action( 'add_meta_boxes_product', 'register_product_metabox' );

function register_product_metabox()
{
    // register the metabox
}

You get the post id in your callback per post object now:

function metabox_callback( $post )
{
    $field = get_post_meta( $post->ID );
}

This happens because WordPress calls the callback with:

call_user_func($box['callback'], $object, $box);

See wp-admin/includes/template.php.

  • But where does the $post argument come from? When I give add_meta_box the callback function, I give it like a string 'myCallbackFucking' and I don't think I can pass an argument like that. – user25893 Jul 20 '13 at 22:22
  • 1
    WordPress sends it when it calls the callback. – fuxia Jul 20 '13 at 22:23
  • Oh ok. And how do we know what are the arguments wordpress passes in the callbacks/functions? Ex: add_action( 'save_post', 'add_movie_review_fields', 10, 2); passes 2 arguments, but what are the 2 arguments? – user25893 Jul 20 '13 at 22:27
  • 1
    You have to look at the source code in WordPress. This is the only reliable source anyway. – fuxia Jul 20 '13 at 22:43

Your Answer

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