diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index 7f9a5460f3..bc084c46fa 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -4196,7 +4196,10 @@ function get_page_uri( $page ) { $uri = $page->post_name; foreach ( $page->ancestors as $parent ) { - $uri = get_post( $parent )->post_name . '/' . $uri; + $parent = get_post( $parent ); + if ( 'publish' === $parent->post_status ) { + $uri = $parent->post_name . '/' . $uri; + } } return $uri; diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index ac2ca71ce5..d6ce0309e6 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -650,6 +650,24 @@ class Tests_Post extends WP_UnitTestCase { $this->assertFalse( get_page_uri( $post_id ) ); } + /** + * @ticket 15963 + */ + function test_get_post_uri_check_orphan() { + $parent_id = $this->factory->post->create( array( 'post_name' => 'parent' ) ); + $child_id = $this->factory->post->create( array( 'post_name' => 'child', 'post_parent' => $parent_id ) ); + + // check the parent for good measure + $this->assertEquals( 'parent', get_page_uri( $parent_id ) ); + + // try the child normally + $this->assertEquals( 'parent/child', get_page_uri( $child_id ) ); + + // now delete the parent and check + wp_delete_post( $parent_id ); + $this->assertEquals( 'child', get_page_uri( $child_id ) ); + } + /** * @ticket 23708 */