Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know I can use get_num_queries() for the number of queries. However, how can I see what queries Wordpress actually makes? I've tried using $query but that didn't work.

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

See this codex page.

in wp-config.php:

define('SAVEQUERIES', true);

then in your template:

if (current_user_can('administrator')){
    global $wpdb;
    echo "<pre>";
    print_r($wpdb->queries);
    echo "</pre>";
}

or without the above SAVEQUERIES, you can still see just the main query:

global $wp_query;
echo $wp_query->request;

or to see all of $wp_query:

<pre>
    <?php print_r($wp_query); ?>
</pre>
share|improve this answer
    
Thanks Milo, much appreciated! –  FLX Aug 21 '11 at 23:13
    
This is useful and the results are shocking, I can't understand why Wordpress makes soooo many useless queries –  jasondavis Apr 21 '12 at 20:49
add comment

Milo's answer is right on. I would add that to save you some trouble, download the WordPress Debug Bar plugin. This plugin will add a little bar to the bottom of your front end pages and it will capture important debugging information, including queries made during the request. For it to work you need to make sure that the constants WP_DEBUG and SAVE_QUERIES are set to true in your wp-config.php file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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