From 3226d8b8677edca7ee362ae003e1c52fc8af8339 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 24 Sep 2014 21:29:15 +0000 Subject: [PATCH] Create correct permalinks for child posts of hierarchical post types when default permalinks are used. props loushou. fixes #29615 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@29765 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 6 ++++-- tests/phpunit/tests/query/results.php | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 7a7b7874ed..13a74aa92a 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -257,10 +257,12 @@ function get_post_permalink( $id = 0, $leavename = false, $sample = false ) { $post_type = get_post_type_object($post->post_type); + if ( $post_type->hierarchical ) { + $slug = get_page_uri( $id ); + } + if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) { if ( ! $leavename ) { - if ( $post_type->hierarchical ) - $slug = get_page_uri($id); $post_link = str_replace("%$post->post_type%", $slug, $post_link); } $post_link = home_url( user_trailingslashit($post_link) ); diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index e3d5872954..9a3e89d264 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -651,4 +651,28 @@ class Tests_Query_Results extends WP_UnitTestCase { $this->assertCount( 1, $result ); } + /** + * @ticket 29615 + */ + function test_child_post_in_hierarchical_post_type_with_default_permalinks() { + global $wp_rewrite; + + $old_permastruct = get_option( 'permalink_structure' ); + $wp_rewrite->set_permalink_structure( '' ); + $wp_rewrite->flush_rules(); + + register_post_type( 'handbook', array( 'hierarchical' => true ) ); + + $post_1 = $this->factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) ); + $post_2 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_1, 'post_type' => 'handbook' ) ); + + $this->assertContains( 'contributing-to-the-wordpress-codex/getting-started', get_permalink( $post_2 ) ); + + $result = $this->q->query( array( 'handbook' => 'contributing-to-the-wordpress-codex/getting-started', 'post_type' => 'handbook' ) ); + $this->assertCount( 1, $result ); + + $wp_rewrite->set_permalink_structure( $old_permastruct ); + $wp_rewrite->flush_rules(); + } + }