Use of Global Variables in WordPress

If you have a variable concept then it is very easy to understand a global veriable. A variable is that in which store the values it may be interger, float, name, etc. Every variable has scopes and boundary or limit, some variable can access only in a function or some variable can access only in a file but some variable can call or access the value in all over the code where you want. So a global variable is a variable that has a defined value that can be accessed anywhere within the WordPress file or execution file.

As I previously told that these variables store all types of information like the database credentials and about the Loop content, author information, and users details, and all the specific information about the WordPress installation. For retrieve data the Global variables should only be used, these variable nerver write directly. If you overwriting the global variable values then it could be cause unknown results in WordPress because most parts of core and extended functionality and all the core files depend on these values means global variable values.

The most values remain consistent for the duration of a query, page load, or single-post handling. After assigning the values to a global variables it almost always has unintended or unwanted side effects of the output, and they’re almost always not what the user or blog author wanted on the output.

Post Data in WordPress

We have read in previous article the Loop is calling the_post(). It call only one time you can have access or fetch to all of the data in WordPress specific to the post being displayed where you want. The global $post variable where this data is stored. In this global variable $post stores the post data of the last post displayed on the page of the site.

Let’s suppose if your Loop displays five posts, then the $post variable will store post data for the fifth post displayed. Below an example is showing that that how you can reference the $post global variable and display all values in the array using the print_r() PHP function or method.


global $post;

print_r( $post ); //view all data stored in the $post array


The output of this code it will print the array values for the $post global variable. So the default WordPress blog post would look like this worked:

stdClass Object


[ID] => 1

[post_author] => 1

[post_date] => 2012-06-09 19:05:19

[post_date_gmt] => 2012-06-09 17:23:50

[post_content] => Welcome to WordPress.

Edit or delete it, then start blogging!

[post_title] => Hello world!

[post_excerpt] =>

[post_status] => publish

[comment_status] => open

[ping_status] => open

[post_password] =>

[post_name] => hello-world

[to_ping] =>

[pinged] =>[post_modified] => 2013-09-01 07:04:15

[post_modified_gmt] => 2012-06-09 19:04:12

[post_content_filtered] =>

[post_parent] => 0

[guid] => http://localhost/Brad/?p=1

[menu_order] => 0

[post_type] => post

[post_mime_type] =>

[comment_count] => 1

[ancestors] => Array



[filter] => raw


Above array you can see the the $post global variable contains all sorts of data for the post like post id, post auther name, post date etc:


global $post;

echo $post->post_title; //display the post title

echo $post->post_content; //display the post content


When you accessing the content through the global $post variable it means that you are accessing the unfiltered content or the post. So the plugin that would normally alter the output of the content will not effect the global content value of the data.

For example, The gallery is short code if you had the built-in gallery shortcode in your post and it to display all images uploaded on the post data, retrieving or fetch the post content as shown would return gallery instead of the actual image gallery. Also remember that WordPress has provided the template tags that can be accessed anywhere to retrieve or fetch these values as well, in most cases, template tags are going to be the preferred mechanism for getting at these bits.For example, You have need to get the permalink of your post data then you can use the following method or function:


global $post;

echo get_permalink( $post->ID ); //displays the posts permalink