This is perhaps too broad a question to answer well. The
$post global could be used in all sorts of ways. Whether or not it should be used depends on each circumstance.
However, as I understand it, the main intended purpose of the
$post global is its use in theme template files.
When you use
the_author() or any of the very many template functions, WordPress goes looking for this information in the
$post global. As a general rule, when working in the template files, you will always want to use these template functions, like
get_the_ID(), instead of accessing
So if you want to understand the
$post global better, you should read up on how The Loop works in WordPress, particularly within the template files. When a Loop runs
the_post(), it is setting up the global with the current item.
In template files, you'll usually be iterating over a Loop for a query that is run automatically. When you access a single Page, for instance, WordPress knows to run a query for that page and load the
If you want to retrieve extra posts outside of the main Loop, for instance in your
functions.php, you would run your own WP_Query, initiate the Loop and then use the template functions. Only access the
$post global directly if you're unable to access the information you want through an existing function.
When you're done looping over your own
WP_Query, you'll find that the
$post global is now stuck on your custom query. So, for instance, on our single page template, after our custom loop, we may no longer have the current page set up in
$post. You can restore the
$post global to its former state with wp_reset_query. This is absolutely critical or else you can break other plugins or themes which expect the original