WP-Mix

A fresh mix of code snippets and tutorials

View all WP Post variables

Quick tip to view all WP Post variables, which can be useful for development and debugging in WordPress.

In the previously covered method of viewing all WP query variables, we display all Post variables in detail using the PHP function var_dump(). The output looks like this:

object(WP_Post)#165 (24) { 
	["ID"]                    => int(2) 
	["post_author"]           => string(1) "1" 
	["post_date"]             => string(19) "2011-08-21 23:31:47" 
	["post_date_gmt"]         => string(19) "2011-08-22 06:31:47" 
	["post_content"]          => string(0) "" 
	["post_title"]            => string(4) "Example Post" 
	["post_excerpt"]          => string(0) "" 
	["post_status"]           => string(7) "publish" 
	["comment_status"]        => string(4) "open" 
	["ping_status"]           => string(6) "closed" 
	["post_password"]         => string(0) "" 
	["post_name"]             => string(4) "example-post" 
	["to_ping"]               => string(0) "" 
	["pinged"]                => string(0) "" 
	["post_modified"]         => string(19) "2012-01-20 22:11:53" 
	["post_modified_gmt"]     => string(19) "2012-01-21 05:11:53" 
	["post_content_filtered"] => string(0) "" 
	["post_parent"]           => int(0) 
	["guid"]                  => string(32) "http://example.com/wp/?page_id=2" 
	["menu_order"]            => int(0) 
	["post_type"]             => string(4) "page" 
	["post_mime_type"]        => string(0) "" 
	["comment_count"]         => string(1) "0" 
	["filter"]                => string(3) "raw" 
}

As you can see, in addition to the name and value of each variable, var_dump() also includes the type, such as “string” or “int”. If you don’t need the type information, you can simplify the results using PHP’s var_export(), like so:

echo var_export($GLOBALS['post'], TRUE);

When included in your theme template, that snippet will output a simplified version of the results returned with var_dump(). To illustrate, here is the variable output for the same page used in the previous example:

WP_Post::__set_state(
	array( 
		'ID'                      => 2, 
		'post_author'             => '1', 
		'post_date'               => '2011-08-21 23:31:47', 
		'post_date_gmt'           => '2011-08-22 06:31:47', 
		'post_content'            => '', 
		'post_title'              => 'Example Post', 
		'post_excerpt'            => '', 
		'post_status'             => 'publish', 
		'comment_status'          => 'open', 
		'ping_status'             => 'closed', 
		'post_password'           => '', 
		'post_name'               => 'example-post', 
		'to_ping'                 => '', 'pinged' => '', 
		'post_modified'           => '2012-01-20 22:11:53', 
		'post_modified_gmt'       => '2012-01-21 05:11:53', 
		'post_content_filtered'   => '', 
		'post_parent'             => 0, 
		'guid'                    => 'http://example.com/wp/?page_id=2', 
		'menu_order'              => 0, 
		'post_type'               => 'page', 
		'post_mime_type'          => '', 
		'comment_count'           => '0', 
		'filter'                  => 'raw', 
	)
)

Further, here is a way to implement the display-variable technique via the theme’s functions.php file:

// View all WP Post variables
function wpmix_display_globals($content) {
	return $content . var_export($GLOBALS['post'], TRUE);
}
add_filter('the_content', 'wpmix_display_globals');

Of course, this can be fine-tuned by using WP’s template tags, such as conditional tags that filter by post type, category and so forth.

More tips on the way!

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress