Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 29710)
+++ src/wp-includes/post.php	(working copy)
@@ -3677,7 +3677,6 @@
 	if ( ! is_array( $feeds ) )
 		$feeds = array();
 
-	$hierarchical_post_types = get_post_types( array('hierarchical' => true) );
 	if ( 'attachment' == $post_type ) {
 		// Attachment slugs must be unique across all types.
 		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
@@ -3700,7 +3699,7 @@
 			} while ( $post_name_check );
 			$slug = $alt_post_name;
 		}
-	} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
+	} elseif ( is_post_type_hierarchical( $post_type ) ) {
 		if ( 'nav_menu_item' == $post_type )
 			return $slug;
 
@@ -3708,8 +3707,8 @@
 		 * Page slugs must be unique within their own trees. Pages are in a separate
 		 * namespace than posts so page slugs are allowed to overlap post slugs.
 		 */
-		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
-		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
+		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1";
+		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
 
 		/**
 		 * Filter whether the post slug would make a bad hierarchical post slug.
Index: tests/phpunit/tests/post.php
===================================================================
--- tests/phpunit/tests/post.php	(revision 29710)
+++ tests/phpunit/tests/post.php	(working copy)
@@ -991,4 +991,29 @@
 		_unregister_taxonomy( $tax );
 	}
 
+	/**
+	 * @ticket 18962
+	 */
+	function test_wp_unique_post_slug_with_multiple_hierarchies() {
+		register_post_type( 'post-type-1', array( 'hierarchical' => true ) );
+		register_post_type( 'post-type-2', array( 'hierarchical' => true ) );
+
+		$args = array(
+			'post_type' => 'post-type-1',
+			'post_name' => 'some-slug',
+			'post_status' => 'publish',
+		);
+		$one = $this->factory->post->create( $args );
+		$args['post_type'] = 'post-type-2';
+		$two = $this->factory->post->create( $args );
+
+		$this->assertEquals( 'some-slug', get_post( $one )->post_name );
+		$this->assertEquals( 'some-slug', get_post( $two )->post_name );
+
+		$this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-1', 0 ) );
+		$this->assertEquals( 'some-other-slug', wp_unique_post_slug( 'some-other-slug', $one, 'publish', 'post-type-2', 0 ) );
+
+		_unregister_post_type( 'post-type-1' );
+		_unregister_post_type( 'post-type-2' );
+	}
 }
