Same query_vars, but different SQL request between main loop and Wp_Query
-
Hello,
while writing a high level interface to WordPress for usage in the templates I ran across a strange problem: Consider the following file
page.php:<?php global $wp_query; $current = $wp_query; var_dump($current->request); $new = new WP_Query($wp_query->query_vars); var_dump($new->request); var_dump(array_diff_assoc($current->query_vars, $new->query_vars));When I open a single page, say with id
5and slugfoobar, I’ll get the same query vars (tested with array_diff_assoc, as well as manually), but two different SQL requests:// $current: string 'SELECT wps13t_posts.* FROM wps13t_posts WHERE 1=1 AND (wps13t_posts.ID = '5') AND wps13t_posts.post_type = 'page' ORDER BY wps13t_posts.post_date DESC ' // $new: string 'SELECT wps13t_posts.* FROM wps13t_posts WHERE 1=1 AND wps13t_posts.post_name = 'foobar' AND wps13t_posts.post_type = 'post' ORDER BY wps13t_posts.post_date DESC 'The first SQL request uses the page id (correct) and post_type post (correct). The 2nd SQL request uses the post_name (correct, too) and post_type page (error!).
I’m wondering why this strange behaviour occurs and whether it’s me or WordPress itself causing this error.
How can I init a WP_Query that acts the same as the default query?
Thanks for your help,
/Olaf
The topic ‘Same query_vars, but different SQL request between main loop and Wp_Query’ is closed to new replies.