WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's probably more of a PHP question, but I would like to know if there are any differences in using

global $post;
echo $post->ID;

when compared to

echo $GLOBALS['post']->ID;

to access the ID of a post in WordPress.

This answer on SO suggests that the first method is quicker to process, while the second method is quicker to write.

I also noticed that the first method is often mentioned in the Codex while the second method is mentioned on the_content filter page.

Is this only a matter of preference? Or does it come to performance and security too?


share|improve this question
Avoid globals when you can. In your case, you can use get_the_ID() instead – onetrickpony Nov 21 '12 at 8:59
Yes, I usually use get_the_ID(), but any particular reason for avoiding using globals? – RRikesh Nov 21 '12 at 10:00
A global variable can be removed or changed in future updates, while a function will always return what you expect. And if it gets deprecated, you'll get notified about this – onetrickpony Nov 21 '12 at 11:14
+1 for this. I haven't thought about this option. – RRikesh Nov 21 '12 at 11:27
up vote 5 down vote accepted

There is no difference when you are using just echo. What works different is unset():

function test_unset_1()
    global $post;
    unset( $post );
function test_unset_2()
    unset( $GLOBALS['post'] );

echo $GLOBALS['post']->ID; // will work

echo $GLOBALS['post']->ID; // will fail

The reason is that unset() destroys just the local reference in the first case and the real global object in the second.

For readability use always $GLOBALS['post']. It is easier to see where the variable is coming from.

share|improve this answer
Thanks Toscho :) – RRikesh Nov 21 '12 at 10:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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