[wp-trac] [WordPress Trac] #5141: wp_the_query is not stable

WordPress Trac wp-trac at lists.automattic.com
Thu Oct 4 13:34:37 GMT 2007

#5141: wp_the_query is not stable
 Reporter:  082net   |       Owner:  anonymous                             
     Type:  defect   |      Status:  new                                   
 Priority:  normal   |   Milestone:  2.3.1                                 
Component:  General  |     Version:  2.3                                   
 Severity:  normal   |    Keywords:  wp_the_query, wp_query, queried_object
 See [http://wordpress.org/support/topic/136624?replies=7#post-626268 my
 comment] on support forum.

 $wp_the_query seems tring to remain first WP_Query but 'custom loop'
 overrides $wp_the_query->post and $wp_the_query->posts.

 Example of 'custom loop' :
 while($myposts->have_posts()): $mypost->the_post(); .... endwhile;
 foreach($myposts as $post) { ... }

 So $wp_the_query->get_queried_object() would not stable until it is
 firstly called before any plugin or theme because it returns pre-set value
 if available.
         function get_queried_object() {
                 if (isset($this->queried_object)) {
                         return $this->queried_object;

 Because almost wordpress theme use 'wp_title()' for <title> </title> and
 wp_title call 'wp_query->get_queried_object()', there seems no problem.

 But if a theme does not use wp_title but something else by hand,
 $wp_the_query->queried_object would remain NULL. So if a page contains
 'custom loop' and when any plugin, theme or wordpress hook call
 '$wp_the_query->get_queried_object()' after the custom loop, it returns
 last post data of the custom loop.

 And I suggest two way to solve this.

 wp-includes/class.php 'WP::query_posts()'
         function query_posts() {
                 global $wp_the_query;
                 $wp_the_query->get_queried_object();// add this line



 wp-includes/class.php 'WP::register_globals()'
         function register_globals() {
                 global $wp_query;
                 // Extract updated query vars back into global namespace.
                 foreach ($wp_query->query_vars as $key => $value) {
                         $GLOBALS[$key] = $value;

                 $GLOBALS['query_string'] = & $this->query_string;
                 $GLOBALS['posts'] = $wp_query->posts;// remove reference
                 $GLOBALS['post'] = $wp_query->post;// remove reference
                 $GLOBALS['request'] = & $wp_query->request;

                 if ( is_single() || is_page() ) {
                         $GLOBALS['more'] = 1;
                         $GLOBALS['single'] = 1;

 I think the first is better one.

Ticket URL: <http://trac.wordpress.org/ticket/5141>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list