Search for the Article

Understanding the global $post Object in WordPress

This code snippet is used to display information about the global $post object in WordPress.

  • global $post;: This line makes the $post object available within the current scope, allowing you to access information about the currently queried post.

  • echo '<pre>';: This HTML tag <pre> is used to define preformatted text. It preserves whitespace and line breaks in the output.

  • print_r($post);: This PHP function print_r() is used to display information about a variable in a human-readable format. In this case, it prints out the contents of the $post object, showing details such as the post ID, author, date, content, and other relevant post data.

  • echo '</pre>';: This HTML tag </pre> closes the <pre> tag opened earlier, ending the preformatted text block.

So, altogether, this code is used to output detailed information about the current post being queried in WordPress, facilitating debugging or inspection of the post data.


echo '<pre>';
print_r($post);
echo '</pre>';