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

Dion Hulse (dd32) wordpress at dd32.id.au
Tue Dec 6 01:35:51 UTC 2011

On 6 December 2011 12:30, IC IC <icwordpress at gmail.com> wrote:
> Mike,
> I tried the snippet as follows;
> <?php
> 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?

The 0=1 is there to select no posts, It's doing that because there are
no posts which match your query.
In the case of Taxonomies and sometimes post meta, multiple queries
will be made in order to build the query.
You'll have to add define('SAVEQUERIES', true); and dump out
$wpdb->queries to see all the queries made.

More information about the wp-hackers mailing list