Have you ever had the, ahem, pleasure of customizing the WordPress query? One of the most useful things in debugging is displaying the last WordPress query.
Let’s say you’re working with taxonomies and/or post types and you’re passing custom parameters around. And then, you want to setup some debugging to see exactly what’s hitting the database.
This is helpful for a variety of reasons if for no other reason than to see what is running against the database. It gives you insight on what you’re requesting and thus what you’re presenting to the user.
Anyway, writing code to do this is easy.
The Last WordPress Query
Before sharing code, I want to stress that I do not think this is a replacement for a true debugger. Furthermore, this also assumes that you have an understanding of SQL.
In some upcoming posts, I’m going to talk about how to customize the query for more advanced use. For now, though, I thought this might be a good starting place.
After all, regardless of what you’re doing with WordPress, queries are running. So why not be able to take a peek at them?
For example, to view the most recently executed search query, you can do this:
Place this in your theme, your plugin, the mu-plugins
directory, or wherever’s most appropriate.
If you want to take this a step further and enable some type of “debug mode” for your project, you can always wrap the action in a query string conditional.
For example, see the following code:
As mentioned, I’ll talk more about customizing a queries in future posts. For now, though, this is something that’s useful regardless of how advanced your work is.
Leave a Reply
You must be logged in to post a comment.