Index: src/wp-includes/query.php
===================================================================
--- src/wp-includes/query.php	(revision 31467)
+++ src/wp-includes/query.php	(working copy)
@@ -3939,6 +3939,13 @@
 			$this->queried_object = get_userdata( $this->queried_object_id );
 		}
 
+		// Setting `queried_object_id` before the query has run causes singular
+		// queries for hierarchical custom post types to fail.
+		if ( $this->is_singular && is_null( $this->queried_object ) ) {
+			// _doing_it_wrong()?
+			unset( $this->queried_object_id );
+		}
+
 		return $this->queried_object;
 	}
 
@@ -4077,6 +4084,10 @@
 
 		$post_obj = $this->get_queried_object();
 
+		if ( ! $post_obj ) {
+			return false;
+		}
+
 		if ( in_array( (string) $post_obj->ID, $attachment ) ) {
 			return true;
 		} elseif ( in_array( $post_obj->post_title, $attachment ) ) {
@@ -4461,6 +4472,10 @@
 
 		$post_obj = $this->get_queried_object();
 
+		if ( ! $post_obj ) {
+			return false;
+		}
+
 		$post = (array) $post;
 
 		if ( in_array( (string) $post_obj->ID, $post ) ) {
@@ -4504,6 +4519,10 @@
 
 		$post_obj = $this->get_queried_object();
 
+		if ( ! $post_obj ) {
+			return false;
+		}
+
 		return in_array( $post_obj->post_type, (array) $post_types );
 	}
 
Index: tests/phpunit/tests/query.php
===================================================================
--- tests/phpunit/tests/query.php	(revision 31467)
+++ tests/phpunit/tests/query.php	(working copy)
@@ -122,4 +122,46 @@
 	public function filter_parse_query_to_remove_tax( $q ) {
 		unset( $q->query_vars['wptests_tax'] );
 	}
+
+	/**
+	 * @ticket 31355
+	 */
+	public function test_pages_dont_404_when_queried_post_id_is_modified() {
+		$post_id = $this->factory->post->create( array( 'post_title' => 'A Test Page', 'post_type' => 'page' ) );
+
+		add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
+
+		$url = get_permalink( $post_id );
+		$this->go_to( $url );
+
+		remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
+
+		$this->assertFalse( $GLOBALS['wp_query']->is_404() );
+		$this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
+	}
+
+	/**
+	 * @ticket 31355
+	 */
+	public function test_custom_hierarchical_post_types_404_when_queried_post_id_is_modified() {
+		global $wp_rewrite;
+
+		register_post_type( 'guide', array( 'name' => 'Guide', 'public' => true, 'hierarchical' => true ) );
+		$wp_rewrite->flush_rules();
+		$post_id = $this->factory->post->create( array( 'post_title' => 'A Test Guide', 'post_type' => 'guide' ) );
+
+		add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
+
+		$url = get_permalink( $post_id );
+		$this->go_to( $url );
+
+		remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
+
+		$this->assertFalse( $GLOBALS['wp_query']->is_404() );
+		$this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
+	}
+
+	public function filter_parse_query_to_modify_queried_post_id( $query ) {
+		$post = get_queried_object();
+	}
 }
