People are often confused about how to get data from global objects/variables

Question: In which ways can you inspect global variables?

This Q was written because it's needed pretty often over here at WA. I just wanted to have it as a fav to link over here (people often don't take a look at github gist links).

Feel free to modify the example if something is wrong or you think that the explanation is missing something. If you want to add other useful stuff, please add each as a single answer. Thank you.

  • This should be a community wiki, or rephrased into a question.
    – t31os
    Mar 25, 2011 at 15:08
  • @t31os Could you do this? I don't even know where i'd find a community wiki...
    – kaiser
    Mar 25, 2011 at 15:32
  • I think i could previously when in beta, not now though, the rep requirements are higher, might need to ask a higher rep user to mark the question as wiki, maybe @Rarst or @MikeSchinkel can..
    – t31os
    Mar 25, 2011 at 15:35
  • @MikeSchinkel @Rarst @Jan Fabry - push
    – kaiser
    Apr 8, 2011 at 13:55

3 Answers 3


Or, if you're lazy, just install the Debug Bar plugin.

It adds a button to the admin bar that, when clicked, reveals a panel with all kinds of useful information, including deprecation notices, WP_Query variables and an SQL query log.

  • Absolutely right. But it won't explain you how to access global variables/objects and how to get parts out of it.
    – kaiser
    Mar 25, 2011 at 15:34
  • Btw: Could you just add a short explanation on how this works? I guess it could/would really help extending this "how-to basics"-AQ.
    – kaiser
    Apr 1, 2011 at 2:19
  • @kaiser: Done .
    – scribu
    Apr 1, 2011 at 2:45

How to inspect the data:

Use this to get an insight view of what you can use from the current request/wp_query.
function inspect_wp_query() 
  echo '<pre>';
  echo '</pre>';
// If you're looking at other variables you might need to use different hooks
// this can sometimes be a little tricky.
// Take a look at the Action Reference: http://codex.wordpress.org/Plugin_API/Action_Reference
add_action('shutdown', 'inspect_wp_query', 999); // Query on public facing pages
add_action('admin_footer', 'inspect_wp_query', 999); // Query in admin UI


    // this:
    global $wp_query;
    // is the same as
    // and as this:

// You can do this with each other global var too, like $post, etc.

How to actually get the data:

// Example (not the best one)
(Object) WP_Query -> post (stdClass) -> postdata (Array)

// How to get the data:
// Save object into var
$my_data = new WP_Query; // on a new object
// or on the global available object from the current request
$my_data = $GLOBALS['wp_query'];

// get object/stdClass "post"
$my_post_data = $my_data->post;
// get Array
$my_post_data = $my_data['post'];


  • Do you also have solution for successfully outputting print_r($GLOBALS) itself? Is that possible? I failed at it, meanwhile var_dump($GLOBALS) did it. Dec 19, 2021 at 21:31
  • 1
    @ViktorBorítás I am not sure what you intend, but you can take a look at the examples link. In any way, I'd recommend using XDebug and hook it up to your IDE (PhpStorm, Aptana, VS Code) to get proper debugging instead.
    – kaiser
    Dec 20, 2021 at 10:44
  • I was trying to print more readable list of all the globals (thus i thought $GLOBALS is an existing object that can be print_r()-ed as well, since var_dump() worked. Now, i'm just staying curious, why it didn't happen (at 'shutdown' hook). Tx for your suggestion about IDE integration tho, surely will go for it too. Dec 20, 2021 at 19:00

Depending on where in process of loading the scripts and rendering the final output, some of the above mentioned variables may not be present. If you want a fairly inclusive view, perhaps a bit extreme, try:


var_dump is also nice in that tell you the type and formats the data a bit.

  • hi, do you know the reason why var_dump() works in this case and print_r() doesn't? (throws 500 server error to me) Dec 19, 2021 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.