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.



git-svn-id: https://develop.svn.wordpress.org/trunk@52194 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2021-11-17 03:23:40 +00:00
parent 6679a0614d
commit aec78f5a16
2 changed files with 12 additions and 2 deletions

View File

@ -7642,12 +7642,13 @@ function _publish_post_hook( $post_id ) {
* Returns the ID of the post's parent.
*
* @since 3.1.0
* @since 5.9.0 The `$post` parameter was made optional.
*
* @param int|WP_Post $post Post ID or post object.
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
* @return int|false Post parent ID (which can be 0 if there is no parent),
* or false if the post does not exist.
*/
function wp_get_post_parent_id( $post ) {
function wp_get_post_parent_id( $post = null ) {
$post = get_post( $post );
if ( ! $post || is_wp_error( $post ) ) {
return false;

View File

@ -33,6 +33,15 @@ class Tests_Post_wpGetPostParentId extends WP_UnitTestCase {
$this->assertSame( self::$parent_post_id, wp_get_post_parent_id( self::$post_id ) );
}
/**
* @ticket 48358
* @covers ::wp_get_post_parent_id
*/
public function test_wp_get_post_parent_id_with_no_post_argument_default_to_global_post_id() {
$GLOBALS['post'] = get_post( self::$post_id );
$this->assertSame( self::$parent_post_id, wp_get_post_parent_id() );
}
public function test_wp_get_post_parent_id_with_non_existing_id_default_to_global_post_id() {
$GLOBALS['post'] = get_post( self::$post_id );
$this->assertSame( self::$parent_post_id, wp_get_post_parent_id( 0 ) );