From f7d7ecd0a6a39224de4fe2711b81a8ef16dc7f30 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 23 Jun 2014 13:14:39 +0000 Subject: [PATCH] Prevent multiple hierarchical posts with the same slug from being displayed in single post template. fixes #28611. git-svn-id: https://develop.svn.wordpress.org/trunk@28803 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 6 +++--- tests/phpunit/tests/query/results.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 7199c9c503..c2421cd369 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2372,11 +2372,11 @@ class WP_Query { if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) ) continue; - if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) { - // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name' + if ( ! $ptype_obj->hierarchical ) { + // Non-hierarchical post types can directly use 'name'. $q['name'] = $q[ $ptype_obj->query_var ]; } else { - // Hierarchical post_types will operate through the + // Hierarchical post types will operate through 'pagename'. $q['pagename'] = $q[ $ptype_obj->query_var ]; $q['name'] = ''; } diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index 4c00552ebe..a71e52757b 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -636,4 +636,19 @@ class Tests_Query_Results extends WP_UnitTestCase { $result11 = $this->q->query( array_merge( $args, array( 'post_password' => 'burrito' ) ) ); $this->assertEqualSets( array( $two, $three ), $result11 ); } + + /** + * @ticket 28611 + */ + function test_duplicate_slug_in_hierarchical_post_type() { + register_post_type( 'handbook', array( 'hierarchical' => true ) ); + + $post_1 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_type' => 'handbook' ) ); + $post_2 = $this->factory->post->create( array( 'post_title' => 'Contributing to the WordPress Codex', 'post_type' => 'handbook' ) ); + $post_3 = $this->factory->post->create( array( 'post_title' => 'Getting Started', 'post_parent' => $post_2, 'post_type' => 'handbook' ) ); + + $result = $this->q->query( array( 'handbook' => 'getting-started', 'post_type' => 'handbook' ) ); + $this->assertEquals( 1, $this->q->post_count ); + } + }