Make WordPress Core

Changeset 34089


Ignore:
Timestamp:
09/12/2015 08:57:06 PM (9 years ago)
Author:
boonebgorges
Message:

Allow setup_postdata() to accept a post ID.

Previously, it accepted only a full post object.

Props sc0ttclark, mordauk, wonderboymusic.
Fixes #30970.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r34073 r34089  
    46174617     *
    46184618     * @since 4.1.0
     4619     * @since 4.4.0 Added the ability to pass a post ID to `$post`.
    46194620     *
    46204621     * @global int             $id
     
    46284629     * @global int             $numpages
    46294630     *
    4630      * @param WP_Post $post Post data.
     4631     * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
    46314632     * @return true True when finished.
    46324633     */
    46334634    public function setup_postdata( $post ) {
    46344635        global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
     4636
     4637        if ( ! ( $post instanceof WP_Post ) ) {
     4638            $post = get_post( $post );
     4639        }
     4640
     4641        if ( ! $post ) {
     4642            return;
     4643        }
    46354644
    46364645        $id = (int) $post->ID;
     
    47024711        if ( ! empty( $this->post ) ) {
    47034712            $GLOBALS['post'] = $this->post;
    4704             setup_postdata( $this->post );
     4713            $this->setup_postdata( $this->post );
    47054714        }
    47064715    }
     
    47704779 *
    47714780 * @since 1.5.0
     4781 * @since 4.4.0 Added the ability to pass a post ID to `$post`.
    47724782 *
    47734783 * @global WP_Query $wp_query
    47744784 *
    4775  * @param object $post Post data.
     4785 * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
    47764786 * @return bool True when finished.
    47774787 */
  • trunk/tests/phpunit/tests/query/setupPostdata.php

    r30976 r34089  
    4545        $this->assertNotEmpty( $p->ID );
    4646        $this->assertSame( $p->ID, $GLOBALS['id'] );
     47    }
     48
     49    /**
     50     * @ticket 30970
     51     */
     52    public function test_setup_by_id() {
     53        $p = $this->factory->post->create_and_get();
     54        setup_postdata( $p->ID );
     55
     56        $this->assertSame( $p->ID, $GLOBALS['id'] );
     57    }
     58
     59    /**
     60     * @ticket 30970
     61     */
     62    public function test_setup_by_fake_post() {
     63        $fake = new stdClass;
     64        $fake->ID = 98765;
     65        setup_postdata( $fake->ID );
     66
     67        // Fails because there's no post with this ID.
     68        $this->assertNotSame( $fake->ID, $GLOBALS['id'] );
     69    }
     70
     71    /**
     72     * @ticket 30970
     73     */
     74    public function test_setup_by_postish_object() {
     75        $p = $this->factory->post->create();
     76
     77        $post = new stdClass();
     78        $post->ID = $p;
     79        setup_postdata( $p );
     80
     81        $this->assertSame( $p, $GLOBALS['id'] );
    4782    }
    4883
Note: See TracChangeset for help on using the changeset viewer.