Access post, page, category and author information in custom template page or embedded PHP script within WordPress

In a custom page you call as a template in WordPress, this is the standard way to access some of the information about the particular post or page being viewed.  This is possible by accessing the WordPress query object, which is already instantiated by WordPress in a global class called $wp_query.  There is much more information you can get from this object, by visiting WordPress.org.  Another interesting aspect of accessing the $wp_query object is that you can override many of the properties and methods within this class to alter the functions, data, and output of the containing page so you can tailor the page for further customization.


$query_vars=$wp_query->query_vars;

$pagename=$query_vars['pagename'];
$category_name=$query_vars['category_name'];

$post_arr=$wp_query->post;
$post_author=$post_arr->post_author;

$url= $url_arr['request_uri']=$_SERVER['REQUEST_URI'];

ob_start();
echo “Pagename: $pagename / Cat: $category_name / Url: $url / Author: $post_author”;
var_dump($wp_query);
$html=ob_get_clean();

echo $html;

For custom pages, do not ever close your files with closing PHP tags with scripts. Please note that different WordPress themes like Genesis may have different functions that will either alter how you can access the $wp_query object and in some instances do not allow accessing it.

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *