How to get and debug WordPress SQL queries

WordPress provides handy way to get all queries and to debug them easily.

To enable queries collecting add the following in your wp-config.php:

define('SAVEQUERIES', true);

Next should be added to your theme footer (wp-content/themes/<your_theme>/footer.php)

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.

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

At the end you should be able to get something like:

134 queries in 0.301 seconds.

and a bunch of SQL queries.



Leave a Reply

Your email address will not be published. Required fields are marked *