How to display SQL query that ran in query?

  • I have come across a function before that displayed the exact SQL code that was used. In a loop for example, but can't remember.

    Can anybody tell me that function?

  • Hi @Keith Donegan:

    If I understand your question correctly I think this is what you are looking for?

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

    $wp_query is a global variable that contains the current query run by the loop. If you run the above code anytime while the loop is still active or even right after the loop it should give you the SQL from the loop. Just make sure you inspect it before letting something else run that uses query_posts() again.

    How to get queries of `$wpdb`? `$GLOBALS['wpdb']->request` not working

    Works even on custom query, `$my_query = new WP_Query([ /* ...some args... */ ]);` => `$my_query->request`

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM

Tags used