Skip to content

Commit 54dfd64

Browse files
committed
Fix setup_postdata() to set the (inside the loop) globals before the_post action is fired. Follow-up from #42814 and [44941].
Props david.binda, spacedmonkey, boonebgorges, birgire, jorbin, azaozz. Merges [45285] from trunk. Fixes #47114 for 5.2. git-svn-id: https://develop.svn.wordpress.org/branches/5.2@45286 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 54ed149 commit 54dfd64

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/wp-includes/class-wp-query.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,6 +4208,17 @@ public function setup_postdata( $post ) {
42084208
$more = $elements['more'];
42094209
$numpages = $elements['numpages'];
42104210

4211+
/**
4212+
* Fires once the post data has been setup.
4213+
*
4214+
* @since 2.8.0
4215+
* @since 4.1.0 Introduced `$this` parameter.
4216+
*
4217+
* @param WP_Post $post The Post object (passed by reference).
4218+
* @param WP_Query $this The current Query object (passed by reference).
4219+
*/
4220+
do_action_ref_array( 'the_post', array( &$post, &$this ) );
4221+
42114222
return true;
42124223
}
42134224

@@ -4298,17 +4309,6 @@ public function generate_postdata( $post ) {
42984309
$multipage = 0;
42994310
}
43004311

4301-
/**
4302-
* Fires once the post data has been setup.
4303-
*
4304-
* @since 2.8.0
4305-
* @since 4.1.0 Introduced `$this` parameter.
4306-
*
4307-
* @param WP_Post $post The Post object (passed by reference).
4308-
* @param WP_Query $this The current Query object (passed by reference).
4309-
*/
4310-
do_action_ref_array( 'the_post', array( &$post, &$this ) );
4311-
43124312
$elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
43134313

43144314
return $elements;

tests/phpunit/tests/query/setupPostdata.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
99

1010
protected $global_data = array();
1111

12+
protected $pages_global;
13+
1214
public function setUp() {
1315
parent::setUp();
1416
return;
@@ -416,4 +418,24 @@ function test_setup_postdata_loop() {
416418
}
417419
}
418420

421+
/**
422+
* @ticket 47114
423+
*
424+
* setup_postdata() should set the globals before `the_post` action is fired.
425+
*/
426+
public function test_the_post_action() {
427+
$post = self::factory()->post->create_and_get();
428+
add_action( 'the_post', array( $this, 'the_post_action_callback' ) );
429+
430+
setup_postdata( $post );
431+
432+
$this->assertEquals( $GLOBALS['pages'], $this->pages_global );
433+
}
434+
435+
/**
436+
* Helpers
437+
*/
438+
public function the_post_action_callback() {
439+
$this->pages_global = $GLOBALS['pages'];
440+
}
419441
}

0 commit comments

Comments
 (0)