WordPress.org

Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#5141 closed defect (bug) (duplicate)

wp_the_query is not stable

Reported by: 082net Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.3
Component: General Keywords: wp_the_query, wp_query, queried_object
Focuses: Cc:

Description

See 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;

or

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;
		$this->build_query_string();
		$wp_the_query->query($this->query_vars);
		$wp_the_query->get_queried_object();// add this line
 	}

OR

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.

Change History (5)

#1 @082net
13 years ago

  • Cc 082net added

#2 @082net
13 years ago

  • Owner changed from anonymous to 082net
  • Status changed from new to assigned

#3 @082net
13 years ago

  • Owner changed from 082net to anonymous
  • Status changed from assigned to new

#4 @082net
13 years ago

  • Resolution set to duplicate
  • Status changed from new to closed

#5 @Nazgul
13 years ago

  • Milestone 2.3.1 deleted
Note: See TracTickets for help on using tickets.