what is the way to see the currently executing query in wordpress?

I am working on wordpress which is new to me
Now I have been provided with site completely designed in wp
But the problem is I am unable to search the query for each functionality/page
I found

$wpdb->get_results

used for getting result from database
also this is not working

Read More
$wpdb->queries

Is there any way to print each query currently executing?.

Please Help.

Related posts

Leave a Reply

9 comments

  1. For $wpdb->queries to work you need to configure in wp-config.php:

    define('SAVEQUERIES', true);
    

    This is highly not recommended for production (heavy performance hit) and so turned off by default.

  2. I often do this when I need to check the current query:

    add_action( 'wp_head', 'show_current_query' );
    
    function show_current_query() {
        global $wp_query;
    
        if ( !isset( $_GET['q'] ) )
            return;
        echo '<textarea cols="50" rows="10">';
        print_r( $wp_query );
        echo '</textarea>';
    }
    

    To show the current query, just add ?q into the current URL.

    This will show the current query (stored in global variable $wp_query), including the SQL query and all other query variables.

  3. The global $wp_query contains everything about the current query, including the arguments, results, contents of the results, and the query itself.

    $wp_query->request should do the job for you here.

  4. You can use the Wp Pear Debug plugin. It will show you a list of the queries executed in the page, the number of seconds those queries took to execute and the php function which initiated the query. Please note that you must save the settings at least once before it begins working.

  5. <?php $wpdb->show_errors(); ?>  // use syntax just before your query 
    <?php $wpdb->print_error(); ?>  // user after your query execution
    

    Ie.

    <?php $wpdb->show_errors();
          $id=$wpdb->update('table_name',$array,$condition);
          $wpdb->print_error();
    ?>