How to Debug WordPress SQL Queries

WP Engine
How to Debug WordPress SQL Queries

Many things can go wrong with a WordPress installation. Your custom PHP can generate an error, they may be a configuration problem, your plug-ins might conflict with each other, and you might run out of RAM, or face some other kind of system environment issue. In the past, I’ve talked about several different ways to tackle each type of problem. 500 errors, how to disable all plug-ins via FTP or through the database, and other solutions to specific problems. In this article however, I’m going to introduce a tool that acts like a Swiss Army knife for all kinds of WordPress errors.

Using this, you can debug and isolate a whole range of problems in your WordPress installation. It’s even great for optimizing your code and isolating SQL queries that are taking up too much processing power. So let’s jump right into it.

Using “Debug Objects”

The Debug Objects plugin was initially named “Debug Queries”. In its latest avatar, it’s much more comprehensive. When you download and install it, you can access the settings page from the “Tools” submenu on the left-hand side of the WordPress administration dashboard.

To get started with debugging, you can make it show the debug info by default, or you can use query parameters to do so. I prefer to use the latter approach. So for example, if you want the plug-in to display its output on a particular page, you simply need to append the “debug” query parameter to whatever URL you’re using. So in the case of my test blog, when I visit the URL:

http://www.webhostingshed.com/?debug

It gives me all the necessary debug information at the end of the page like this:

debug parameter

Showing PHP Warnings

Even if your WordPress installation is working fine, you still might be generating PHP warnings that you don’t know about. Normally, these are available in the error log of your web hosting provider. But using this plug-in, you can get a single view of all warnings. For example when I first started up the plug-in and analyzed a page, I got the following warning that I didn’t even know existed:

locate warnings and errors

In this case, it’s a relatively harmless warning as a result of PHP running in strict mode. It means that variable declarations are required, otherwise the code throws a warning. It’s relatively simple to fix and pretty harmless even if left alone. But you personally might encounter something a little more troubling that can cause problems for you down the line.

In this way, the “Debug Objects” add-on is a great way to periodically scan your site for any poorly written code that you should take care of in advance.

Showing SQL Queries

Sometimes, you need to debug SQL queries. Perhaps you’ve written a piece of code constructing an SQL query and are wondering why it’s not giving the expected result. Or perhaps you want to check and see whether or not a particular query is taking up too much time to execute. In the settings page of this plug-in, you can place a checkmark next to the “DB Query” box:

db query

This will bring up three tabs in the debug output where you can see the results and execution times of queries from your plug-ins as well as all the global ones required to render the page:

queries from plugins

Disabling Plugins

One of the most useful functions of the “Debug Objects” plug-in is the ability to use a query parameter to disable all plug-ins of the site and run WordPress in what is called “Safe Mode”. We can enable this by placing a checkmark next to the appropriate setting here:

And by using the following query parameter, we can run WordPress in safe mode:

http://www.webhostingshed.com/?default

This greatly helps with debugging when you suspect a certain plug-in of having crashed your site and you’re unable to load WordPress at all. As mentioned earlier, the plug-in is an all-purpose tool that serves to debug a wide variety of WordPress issues. As a result, it’s worth having around on your website – particularly if you’re a developer.

Leave a Reply

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.