Tag Archives: wp_query

Don’t enable SAVEQUEIRES in production WordPress sites

One of the common ways to debug DB queries in WordPress is to enable SAVEQUERIES, which keeps track of all the DB queries that are executed in WordPress, together with other information like how long it took for the query to execute, what are the functions that called it etc and stores in the global array variable $wpdb->queries.

While this is a great way to debug DB queries, this could be a huge issue on production sites.

Continue reading »

Posted in WordPress | Tagged , | Leave a comment

Get the SQL query generated by WordPress

One of the good things about WordPress is that it provides a nice abstraction to DB and you can query pretty much anything from the DB, by using the appropriate functions instead of writing the query yourself.

All though it is good, there are times when you want to see what was the actual SQL query that got generated. I faced this senarious myself, when I was debugging an issue in my Bulk Delete WordPress plugin.

It turns out that it is quite easy to do it.

The WP_Query object has a method called request that returns the SQL query that was generated.

So if you want to get the query generated by the global $wpdb object then you can use the following code

<?php echo $GLOBALS['wp_query']->request; ?>

On the other hand, if you are using a custom WP_Query object then you can call the request method on your object.

<?php
$my_query = new WP_Query();
echo $my_Query->request;
?>

Posted in WordPress | Tagged , , | 1 Comment