Index: src/wp-includes/theme.php
===================================================================
--- src/wp-includes/theme.php	(revision 39524)
+++ src/wp-includes/theme.php	(working copy)
@@ -1652,7 +1652,10 @@

 		if ( $post_id > 0 && get_post( $post_id ) ) {
 			$post = get_post( $post_id );
-		} else {
+		}
+
+		// `-1` indicates no post exists; no query necessary.
+		if ( ! $post && ( -1 !== $post_id ) ) {
 			$query = new WP_Query( $custom_css_query_vars );
 			$post = $query->post;
 			/*
@@ -1659,11 +1662,7 @@
 			 * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
 			 * @todo This should get cleared if a custom_css post is added/removed.
 			 */
-			if ( $post ) {
-				set_theme_mod( 'custom_css_post_id', $post->ID );
-			} elseif ( -1 !== $post_id ) {
-				set_theme_mod( 'custom_css_post_id', -1 );
-			}
+			set_theme_mod( 'custom_css_post_id', ( $post ) ? $post->ID : -1 );
 		}
 	} else {
 		$query = new WP_Query( $custom_css_query_vars );
@@ -1787,9 +1786,13 @@
 	} else {
 		$r = wp_insert_post( wp_slash( $post_data ), true );

-		// Trigger creation of a revision. This should be removed once #30854 is resolved.
-		if ( ! is_wp_error( $r ) && 0 === count( wp_get_post_revisions( $r ) ) ) {
-			wp_save_post_revision( $r );
+		if ( ! is_wp_error( $r ) ) {
+			set_theme_mod( 'custom_css_post_id', $r );
+
+			// Trigger creation of a revision. This should be removed once #30854 is resolved.
+			if ( 0 === count( wp_get_post_revisions( $r ) ) ) {
+				wp_save_post_revision( $r );
+			}
 		}
 	}

Index: tests/phpunit/tests/customize/custom-css-setting.php
===================================================================
--- tests/phpunit/tests/customize/custom-css-setting.php	(revision 39524)
+++ tests/phpunit/tests/customize/custom-css-setting.php	(working copy)
@@ -135,6 +135,8 @@
 		) );
 		$twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' );

+		remove_theme_mod( 'custom_css_post_id' );
+
 		$this->assertEquals( $post_id, wp_get_custom_css_post()->ID );
 		$this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID );
 		$this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID );
@@ -221,6 +223,29 @@
 	}

 	/**
+	 * Test that wp_get_custom_css_post() doesn't query for a post after caching a failed lookup.
+	 *
+	 * @ticket 39259
+	 */
+	function test_get_custom_css_post_queries_after_failed_lookup() {
+		set_theme_mod( 'custom_css_post_id', -1 );
+		$queries_before = get_num_queries();
+		wp_get_custom_css_post();
+		$this->assertSame( get_num_queries(), $queries_before );
+	}
+
+	/**
+	 * Test that wp_update_custom_css_post() updates the 'custom_css_post_id' theme mod.
+	 *
+	 * @ticket 39259
+	 */
+	function test_update_custom_css_updates_theme_mod() {
+		set_theme_mod( 'custom_css_post_id', -1 );
+		$post = wp_update_custom_css_post( 'body { background: blue; }' );
+		$this->assertSame( $post->ID, get_theme_mod( 'custom_css_post_id' ) );
+	}
+
+	/**
 	 * Test crud methods on WP_Customize_Custom_CSS_Setting.
 	 *
 	 * @covers WP_Customize_Custom_CSS_Setting::value()
@@ -237,6 +262,7 @@
 			'post_status' => 'publish',
 			'post_type' => 'custom_css',
 		) );
+		remove_theme_mod( 'custom_css_post_id' );
 		$this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() );

 		$this->wp_customize->set_post_value( $this->setting->id, '/*overridden*/' );
