SDAC Blog

Read our latest blog posts, learn something new, find answers, and stay up to date.

Analyze Your WordPress Queries

There are many ways to debug and optimize WordPress, and here is one more of my favorites: save queries for later analysis (from the Codex). If you are using a complex theme, several plugins, or even a few custom plugins – you may might notice your site is either slow, sluggish, or just seems to not load as fast as you would like.

Generally people will install a free theme or plugin(s) (I have seen sites with over 100 plugins) and think “wow – I have every possible custom functionality in place and I did not have to do any custom development!” Then the same proud individual will contact us because their site is not performing as they would like.

One of the first things we do is take a look at all the queries that are used on each page load and we do that by editing two files:

wp-config.php
define( ‘SAVEQUERIES’, true );

footer.php

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

Please note – these changes should only be used when debugging. If you keep them in place – it will have a definite impact on performance.

Once you have the changes in place (assuming you are logged in as the administrator) you will be able to view each query, what function called it, and how long the query took to execute.

From the output – you should easily be able to see what queries are most expensive, what plugins you might need to re-consider, etc.