Using global $post v/s $GLOBALS[‘post’]

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'] );
}

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

test_unset_2();
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.

Leave a Comment