Opened 15 years ago
Closed 15 years ago
#11461 closed defect (bug) (worksforme)
When creating a new WP_Query, running the_post() on the new object resets global $post
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
This happens when running multiple loops in a page. If a new WP_Query object is created, $post should only be changed within the scope of that object, right?
To replicate:
// in a template called from a page echo $post->ID; // the ID of the page $new_obj = new WP_Query(); $new_obj->query("some valid querystring"); if ($new_obj->have_posts()) : while ($new_obj->have_posts()) : $new_obj->the_post(); // .. do your loop stuff endwhile; endif; echo $post->ID; // not the same as the first one
Suggested patch attached.
Attachments (1)
Change History (6)
#1
@
15 years ago
$post is a global central to a lot of WP functionality. What you're trying to do can be done via numerous multiple loop solutions. See http://codex.wordpress.org/The_Loop#Multiple_Loops.
#2
@
15 years ago
- Keywords close added; the_post wp_query scope post removed
What nacin said and also, with your patch, how would the global $post be populated in the main query?
Suggest closing as 'worksforme'.
#3
@
15 years ago
I like the general idea of trying to move the WP_Query away from being overly dependent on globals. Unfortunately there are huge backwards-compatibility issues. Practically everybody assumes $post will be defined in the scope of a template file. Lots of themes and theme examples use $post->ID
to get the current post's ID within the Loop.
Someday maybe.
Patch to fix scope issue on the_post