Accessing $post global on a custom post type archive page
Put in the following line at the top of archive page : global $post; Thing is you need to access the global $post object before trying to use it.
Put in the following line at the top of archive page : global $post; Thing is you need to access the global $post object before trying to use it.
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 … Read more
Thought it might be useful to summarise some of the points from the comments here. (Note: ‘Singleton’ is a name given to the pattern of storing an instance of a class in a static property of a class, usually the same one, so here ‘singleton’ and ‘static property’ are used more or less interchangeably) Singleton … Read more
In essence, for all technical purposes, get_post() == $post == $GLOBALS[‘post’] As @tosho already explained in the linked post, $post === $GLOBALS[‘post’], so I will not go into that. What we are interested in is, how is get_post() the same. For this, we will need to look at the source code. As you can see, … Read more
Or, if you’re lazy, just install the Debug Bar plugin. It adds a button to the admin bar that, when clicked, reveals a panel with all kinds of useful information, including deprecation notices, WP_Query variables and an SQL query log.