WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for an solution how I can count and display all queries in a WordPress site. Does anybody know, if there is an good plugin?

Otherwise it would be an solution to check the queries on the console, because I'm working a lot with the console.

share|improve this question
up vote 8 down vote accepted

You can paste this block of code in your currently active WordPress theme functions.php file:

function wpse_footer_db_queries(){
    echo '<!-- '.get_num_queries().' queries in '.timer_stop(0).' seconds. -->'.PHP_EOL;
add_action('wp_footer', 'wpse_footer_db_queries');

The above block of code, will render an HTML comment in the footer of your theme (before </body> and </html>, containing the number of database queries and how log they took to retrieve.

share|improve this answer
That helps me. Is there also an solution to display the queries with its content? Like so: SELECT * FROM wp_posts Thank you – derpiet Oct 29 '12 at 15:30
@derpiet See @toscho's answer. – Michael Ecklund Oct 29 '12 at 16:09

Add …

define( 'SAVEQUERIES', TRUE );

… to your wp-config.php, and inspect $wpdb->queries at shutdown. That is the latest hook and the only one after which no queries are fired. Plus, it works on wp-admin/ too.

Sample code as a plugin:

 * Plugin Name: T5 Inspect Queries
 * Description: Adds a list of all queries at the end of each file.
 * Add the following to your wp-config.php:

define( 'WP_DEBUG',         TRUE );
define( 'SAVEQUERIES',      TRUE );


add_action( 'shutdown', 't5_inspect_queries' );

 * Print a list of all database queries.
 * @wp-hook shutdown
 * @return  void
function t5_inspect_queries()
    global $wpdb;

    $list = '';

    if ( ! empty( $wpdb->queries ) )
        $queries = array ();
        foreach ( $wpdb->queries as $query )
            $queries[] = sprintf(
                '<li><pre>%1$s</pre>Time: %2$s sec<pre>%3$s</pre></li>',
                nl2br( esc_html( $query[0] ) ),
                number_format( sprintf('%0.1f', $query[1] * 1000), 1, '.', ',' ),
                esc_html( implode( "\n", explode(', ', $query[2] ) ) )

        $list = '<ol>' . implode( '', $queries ) . '</ol>';

        '<style>pre{white-space:pre-wrap !important}</style>
        <div class="%1$s"><p><b>%2$s Queries</b></p>%3$s</div>',


After thinking a little bit longer about it I have written another plugin more suited for my needs – and probably yours if you prefer the console.

 * Plugin Name: T5 Log Queries
 * Description: Writes all queries to '/query-log.sql'.
 * Plugin URI:  http://wordpress.stackexchange.com/a/70853/73
 * Version:     2012.11.04
 * Author:      Thomas Scholz
 * Author URI:  http://toscho.de
 * Licence:     MIT

add_filter( 'query', 't5_log_queries' );

 * Write the SQL to a file.
 * @wp-hook query
 * @param   string $query
 * @return  string Unchanged query
function t5_log_queries( $query )
    static $first = TRUE;
    // Change the path here.
    $log_path = apply_filters(
        ABSPATH . 'query-log.sql'
    $header = '';

    if ( $first )
        $time    = date( 'Y-m-d H:i:s' );
        $request = $_SERVER['REQUEST_URI'];
        $header  = "\n\n# -- Request URI: $request, Time: $time ------------\n";
        $first   = FALSE;

    file_put_contents( $log_path, "$header\n$query", FILE_APPEND | LOCK_EX );

    return $query;

Track the file with tail (available on Windows if Git is installed):

$ tail -f query-log.sql -n 50
share|improve this answer

Your Answer


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.