I am confusing to use $post global variable. Is it use inside loop or outside loop? What is the advantage to use it inside and outside loop?

  • 1
    Edit your title because the question should be in the content not the title. Use title like this: $post and where to use it. or something similar
    – aifrim
    Sep 17 '14 at 17:02

The $post global is set by $wp_query->the_post(). As $post is set by the main query and the fact that templates are set within the global variable scope, it is available throughout the template, so there is no need to explicitly calling the $post global when you are within the specified template as the WP_Post objects can be accessed any where. You can access the WP_Post objects by just simply calling them directly, for example $post->ID to retrieve the page ID on a page template

When you need to access post data outside of a template, for example, in a function or in a widget, you need to call the $post global to make the post data available for use.


Within the loop you have functions like:

  • the_ID() or get_the_ID() that return $post->ID
  • the_title() or get_the_title() that return $post->post_title
  • the_content() or get_the_content() that return $post->post_content


But they apply filters and shortcodes for the post you are viewing. For more details on how these functions work visit the WordPress Code Reference

Within a loop you should ALWAYS use the functions that are created for you. They have a reason. As I said before: they apply filters and parse shortcodes. You should also read the Global Variable article in the WordPress codex.

In my own opinion you should only use $post only when you have no other option. For example: You want, for some reason, the unfiltered content of the post.

Within The Loop the functions described above and their counterparts return information of the current post in the loop. If you are on a category/archive page using $post outside the loop will give you the information of the last $post in the loop(if used after) or nothing/what-ever-query happened before that initialized the value.

NOTE this is a global variable that changes within The Loop and if you are using it/or a plugin uses it like I did once (if ($pageposts) : global $post; foreach ($pageposts as $post) : setup_postdata($post); ... endforeach; endif;) it will change it again because of its description and intended purpose:

 * Set up global post data.
 * @since 1.5.0
 * @param object $post Post data.
 * @uses do_action_ref_array() Calls 'the_post'
 * @return bool True when finished.

I used setup_postdata($post) in order to use the $posts designed functions within a carousel I was building that selected posts that belonged to custom post types based on post meta information, current date and etc..

My advice, if you are new to WordPress, stick with the functions implemented for you. Abstractization has its perks. It makes things easier to find, remember and use.

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.