Make WordPress Core

Ticket #24785: 24785.4.diff

File 24785.4.diff, 1.7 KB (added by ericlewis, 12 years ago)
  • tests/phpunit/tests/query.php

     
    5757                        }
    5858                }
    5959        }
     60
     61        /**
     62         * @ticket 24785
     63         *
     64         */
     65        function test_nested_loop_reset_postdata() {
     66                $post_id = $this->factory->post->create();
     67                $nested_post_id = $this->factory->post->create();
     68
     69                $first_query = new WP_Query( array( 'post__in' => array( $post_id ) ) );
     70                while ( $first_query->have_posts() ) { $first_query->the_post();
     71                        $second_query = new WP_Query( array( 'post__in' => array( $nested_post_id ) ) );
     72                        while ( $second_query->have_posts() ) {
     73                                $second_query->the_post();
     74                                $this->assertEquals( get_the_ID(), $nested_post_id );
     75                        }
     76                        $first_query->reset_postdata();
     77                        $this->assertEquals( get_the_ID(), $post_id );
     78                }
     79
     80        }
    6081}
     82 No newline at end of file
  • src/wp-includes/query.php

     
    115115 */
    116116function wp_reset_postdata() {
    117117        global $wp_query;
    118         if ( !empty($wp_query->post) ) {
    119                 $GLOBALS['post'] = $wp_query->post;
    120                 setup_postdata($wp_query->post);
    121         }
     118        $wp_query->reset_postdata();
    122119}
    123120
    124121/*
     
    36283625                global $wp_the_query;
    36293626                return $wp_the_query === $this;
    36303627        }
     3628
     3629        /**
     3630         * After looping through a nested query, this function
     3631         * restores the $post global to the current post in this query
     3632         *
     3633         * @since 3.7.0
     3634         *
     3635         * @return bool
     3636         */
     3637        function reset_postdata() {
     3638                if ( ! empty( $this->post ) ) {
     3639                        $GLOBALS['post'] = $this->post;
     3640                        setup_postdata( $this->post );
     3641                }
     3642        }
    36313643}
    36323644
    36333645/**