I want to get post ID within Post page. I need this for custom meta box so i can set the id in shortcode. I can show the result in notices but don't know how to make it global to use for meta input. What i got till now is :

function foo(){
global $post;
$nid = $post->ID;
add_action( 'admin_notices', 'foo' );

which show the id but i need it as a variable so i can use it later. so i tried to make $nid global then echo it but it show nothing.

  • Please share the meta box code. Commented Feb 14, 2014 at 6:21
  • How are you echoing it? echo $nid; or echo $GLOBALS["nid"]? Commented Feb 14, 2014 at 7:04
  • tried both with lo luck.
    – IqbalBary
    Commented Feb 14, 2014 at 7:32
  • Solved the problem with get_the_ID() within meta box.
    – IqbalBary
    Commented Feb 14, 2014 at 7:42

1 Answer 1


use get_the_ID() to get post id

  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Feb 14, 2014 at 8:48

Your Answer

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

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