[wp-hackers] Seeing the Actual SQL statements WP is running when compiling the posts...

IC IC icwordpress at gmail.com
Tue Dec 6 01:30:31 UTC 2011

I tried the snippet as follows;


include 'wp-load.php';
$query = new WP_Query('cat=1&tag=apples+oranges');
echo $query->request;

and that printed out this;

//SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND 0
= 1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status =
'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT
0, 9

my two questions here are

//where is the word category in this SQL? it's like query_posts not
being tracked.
//and more interestingly what's 0=1 part doing in there. Wouldn't it
make sure that where part matches nothing?


On Fri, Dec 2, 2011 at 8:21 PM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> On Dec 2, 2011, at 11:02 PM, IC IC wrote:
>> Is there a way to see/output the actual SQL query wp runs for
>> compiling the current page record(s)?
>> For learning purposes, I'd like to see what SQL query runs when we say
>> query_posts( 'cat=1&tag=apples+apples' );
>> or when I am serving a taxonomy page such as
>> mysite.com/fruits/apple
>> what's the SQL query WP is running? Can I hook into that section and
>> output the SQL statement on the screen?
> What dd32 said, or here is a standalone file (maybe name it "test.php"?) which can run in your website's root to display the SQL query you ask for:
> <?php
> include 'wp-load.php';
> $query = new WP_Query('cat=1&tag=apples+apples');
> echo $query->request;
> -Mike
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list