Introduce wp_reset_postdata(). Use it to reset the post global for the current query_posts() call after using a loop with a new WP_Query object. Fixes #12320 props scribu.

git-svn-id: http://svn.automattic.com/wordpress/trunk@14607 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2010-05-13 20:39:54 +00:00
parent 742db4bc22
commit 08fc51b214
2 changed files with 14 additions and 1 deletions

View File

@ -554,7 +554,9 @@ class WP_Widget_Recent_Posts extends WP_Widget {
</ul> </ul>
<?php echo $after_widget; ?> <?php echo $after_widget; ?>
<?php <?php
wp_reset_query(); // Restore global post data stomped by the_post(). // Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif; endif;
$cache[$args['widget_id']] = ob_get_flush(); $cache[$args['widget_id']] = ob_get_flush();

View File

@ -75,6 +75,17 @@ function &query_posts($query) {
function wp_reset_query() { function wp_reset_query() {
unset($GLOBALS['wp_query']); unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query']; $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
wp_reset_postdata();
}
/**
* After looping through a separate query, this function restores
* the $post global to the current post in the main query
*
* @since 3.0.0
* @uses $wp_query
*/
function wp_reset_postdata() {
global $wp_query; global $wp_query;
if ( !empty($wp_query->post) ) { if ( !empty($wp_query->post) ) {
$GLOBALS['post'] = $wp_query->post; $GLOBALS['post'] = $wp_query->post;