diff --git src/wp-includes/rewrite.php src/wp-includes/rewrite.php
index 705e5f2bc8..1a1f31847a 100644
--- src/wp-includes/rewrite.php
+++ src/wp-includes/rewrite.php
@@ -471,6 +471,12 @@ function url_to_postid( $url ) {
 	 */
 	$url = apply_filters( 'url_to_postid', $url );
 
+	// Bail early when URL doesn't belong to this site.
+	$host = parse_url( home_url(), PHP_URL_HOST );
+	if ( false === strpos( $url, $host ) ) {
+		//return 0;
+	}
+
 	// First, check to see if there is a 'p=N' or 'page_id=N' to match against
 	if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) )	{
 		$id = absint($values[2]);
diff --git tests/phpunit/tests/rewrite.php tests/phpunit/tests/rewrite.php
index 5f7514915e..014a6c1e76 100644
--- tests/phpunit/tests/rewrite.php
+++ tests/phpunit/tests/rewrite.php
@@ -389,4 +389,18 @@ class Tests_Rewrite extends WP_UnitTestCase {
 		$this->assertInternalType( 'array', $rewrite_rules );
 		$this->assertNotEmpty( $rewrite_rules );
 	}
+
+	/**
+	 * @ticket 39373
+	 */
+	public function test_url_to_postid_should_bail_when_host_does_not_match() {
+		$this->set_permalink_structure( '/%postname%/' );
+
+		$post_id = self::factory()->post->create( array( 'post_name' => 'foo-bar-baz' ) );
+		$permalink = get_permalink( $post_id );
+		$url = str_replace( home_url(), 'http://some-other-domain.com/', get_permalink( $post_id ) );
+
+		$this->assertSame( $post_id, url_to_postid( $permalink ) );
+		$this->assertSame( 0, url_to_postid( $url ) );
+	}
 }
