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. – Chittaranjan Feb 14 '14 at 6:21
  • How are you echoing it? echo $nid; or echo $GLOBALS["nid"]? – Sudeep K Rana Feb 14 '14 at 7:04
  • tried both with lo luck. – IqbalBary Feb 14 '14 at 7:32
  • Solved the problem with get_the_ID() within meta box. – IqbalBary Feb 14 '14 at 7:42

use get_the_ID() to get post id

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

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.