Get executed queries Edit on GitHub In category: WordPress

Getting executed queries #query-logging

Getting full list of SQL queries executed during a request could be very helpful in debugging. Luckily, WordPress provides convinient way to achieve it.

First we need to enable query logging in wp-config.php. More specifically, we need to set SAVEQUERIES constant to true. Place code like this somewhere in the config file:

define('SAVEQUERIES', true);

Then you can use $wpdb->queries property to obtain information about the executed queries. $wpdb is a global object of WordPress database class, so you probably have it available.

// Make a classic var_dump()

// Or echo just SQL commands
foreach($wpdb->queries as $query) {
    echo $query[0] . "\n";

The array is multidimensional - entry for each query consists of SQL command itself, time of the execution and caller.

Comments #comments