Make WordPress Core

Changeset 52194


Ignore:
Timestamp:
11/17/2021 03:23:40 AM (3 years ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Use global post as the default for wp_get_post_parent_id().

Convert the $post parameter of wp_get_post_parent_id() to optional, defaulting to the current global post object when called within the loop.

Props danielpost, davidbaumwald, SergeyBiryukov, birgire, audrasjb, hellofromTonya, TimothyBlynJacobs.
Fixes #48358.

Location:
trunk
Files:
2 edited

Legend:

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

    r52158 r52194  
    76437643 *
    76447644 * @since 3.1.0
    7645  *
    7646  * @param int|WP_Post $post Post ID or post object.
     7645 * @since 5.9.0 The `$post` parameter was made optional.
     7646 *
     7647 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
    76477648 * @return int|false Post parent ID (which can be 0 if there is no parent),
    76487649 *                   or false if the post does not exist.
    76497650 */
    7650 function wp_get_post_parent_id( $post ) {
     7651function wp_get_post_parent_id( $post = null ) {
    76517652    $post = get_post( $post );
    76527653    if ( ! $post || is_wp_error( $post ) ) {
  • trunk/tests/phpunit/tests/post/wpGetPostParentId.php

    r49603 r52194  
    3434    }
    3535
     36    /**
     37     * @ticket 48358
     38     * @covers ::wp_get_post_parent_id
     39     */
     40    public function test_wp_get_post_parent_id_with_no_post_argument_default_to_global_post_id() {
     41        $GLOBALS['post'] = get_post( self::$post_id );
     42        $this->assertSame( self::$parent_post_id, wp_get_post_parent_id() );
     43    }
     44
    3645    public function test_wp_get_post_parent_id_with_non_existing_id_default_to_global_post_id() {
    3746        $GLOBALS['post'] = get_post( self::$post_id );
Note: See TracChangeset for help on using the changeset viewer.