Show executed queries in WordPress with SAVEQUERIES

Once I needed to know which queries were being executed by WordPress. At that moment I configured MySQL to log all the queries and then checked that log until I found the problem and solved it. But it was not as easy as the solution I found today.

You can just put this line in wp-config.php:

define( 'SAVEQUERIES', true );

And then show the queries wherever you like:

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

Then you’ll see something like this:

(
    [0] => Array
        (
            [0] => SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'
            [1] => 0.00034117698669434
            [2] => require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), wp_not_installed, is_blog_installed, wp_load_alloptions
        )

    [1] => Array
        (
            [0] => SELECT * FROM wp_users WHERE user_login = 'admin'
            [1] => 0.00015687942504883
            [2] => require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), WP->init, wp_get_current_user, get_currentuserinfo, apply_filters('determine_current_user'), call_user_func_array, wp_validate_logged_in_cookie, wp_validate_auth_cookie, get_user_by, WP_User::get_data_by
        )
...
)

Ref: http://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis

You might also like

Remove a WordPress action in child theme
If you want to remove a WordPress action that was added in the parent theme like this:function...

Exit maintenance mode in WordPress when an update fails
To exit maintenance mode in WordPress when an update fails is as easy as deleting the .maintenance file...

Fix plugins and themes installation when WordPress asks for FTP credentials
When WordPress doesn't have the right premissions on the wp-content folder it will ask for your FTP credentials...

Change WordPress table prefix to avoid SQL injection attacks
WordPress uses a table prefix for each installation in case you want/need to have more than one WordPress...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.