Allow setup_postdata()
to accept a post ID.
Previously, it accepted only a full post object. Props sc0ttclark, mordauk, wonderboymusic. Fixes #30970. git-svn-id: https://develop.svn.wordpress.org/trunk@34089 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
4ebab4dc16
commit
dcbd8c6c3d
@ -4616,6 +4616,7 @@ class WP_Query {
|
||||
* Set up global post data.
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @since 4.4.0 Added the ability to pass a post ID to `$post`.
|
||||
*
|
||||
* @global int $id
|
||||
* @global WP_User $authordata
|
||||
@ -4627,12 +4628,20 @@ class WP_Query {
|
||||
* @global int $more
|
||||
* @global int $numpages
|
||||
*
|
||||
* @param WP_Post $post Post data.
|
||||
* @param WP_Post|object|int $post WP_Post instance or Post ID/object.
|
||||
* @return true True when finished.
|
||||
*/
|
||||
public function setup_postdata( $post ) {
|
||||
global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
|
||||
|
||||
if ( ! ( $post instanceof WP_Post ) ) {
|
||||
$post = get_post( $post );
|
||||
}
|
||||
|
||||
if ( ! $post ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = (int) $post->ID;
|
||||
|
||||
$authordata = get_userdata($post->post_author);
|
||||
@ -4701,7 +4710,7 @@ class WP_Query {
|
||||
public function reset_postdata() {
|
||||
if ( ! empty( $this->post ) ) {
|
||||
$GLOBALS['post'] = $this->post;
|
||||
setup_postdata( $this->post );
|
||||
$this->setup_postdata( $this->post );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4769,10 +4778,11 @@ function wp_old_slug_redirect() {
|
||||
* Set up global post data.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 4.4.0 Added the ability to pass a post ID to `$post`.
|
||||
*
|
||||
* @global WP_Query $wp_query
|
||||
*
|
||||
* @param object $post Post data.
|
||||
* @param WP_Post|object|int $post WP_Post instance or Post ID/object.
|
||||
* @return bool True when finished.
|
||||
*/
|
||||
function setup_postdata( $post ) {
|
||||
|
@ -46,6 +46,41 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
|
||||
$this->assertSame( $p->ID, $GLOBALS['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30970
|
||||
*/
|
||||
public function test_setup_by_id() {
|
||||
$p = $this->factory->post->create_and_get();
|
||||
setup_postdata( $p->ID );
|
||||
|
||||
$this->assertSame( $p->ID, $GLOBALS['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30970
|
||||
*/
|
||||
public function test_setup_by_fake_post() {
|
||||
$fake = new stdClass;
|
||||
$fake->ID = 98765;
|
||||
setup_postdata( $fake->ID );
|
||||
|
||||
// Fails because there's no post with this ID.
|
||||
$this->assertNotSame( $fake->ID, $GLOBALS['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30970
|
||||
*/
|
||||
public function test_setup_by_postish_object() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$post = new stdClass();
|
||||
$post->ID = $p;
|
||||
setup_postdata( $p );
|
||||
|
||||
$this->assertSame( $p, $GLOBALS['id'] );
|
||||
}
|
||||
|
||||
public function test_authordata() {
|
||||
$u = $this->factory->user->create_and_get();
|
||||
$p = $this->factory->post->create_and_get( array(
|
||||
|
Loading…
Reference in New Issue
Block a user