diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index 212ddde..3f9f821 100644
--- a/tests/phpunit/tests/rest-api/rest-posts-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php
@@ -3725,6 +3725,57 @@ Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco
 		$this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 );
 	}
 
+	/**
+	 * Tests that creating an unpublished post with a slug does not access
+	 * undefined properties on the prepared post object.
+	 *
+	 * `prepare_item_for_database()` sets an `ID` property only when the request
+	 * carries an ID, which is never the case when creating, and sets
+	 * `post_parent` only for hierarchical post types, which `post` is not.
+	 * Creating a draft or pending post with a slug therefore reaches
+	 * `wp_unique_post_slug()` through two undefined property accesses.
+	 *
+	 * @ticket 64921
+	 *
+	 * @dataProvider data_create_item_with_slug_and_unpublished_status
+	 *
+	 * @covers WP_REST_Posts_Controller::create_item
+	 *
+	 * @param string $status The post status to create the post with.
+	 */
+	public function test_create_item_with_slug_and_unpublished_status( $status ) {
+		wp_set_current_user( self::$editor_id );
+
+		$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
+		$request->set_body_params(
+			array(
+				'title'  => 'Unpublished post with a slug',
+				'status' => $status,
+				'slug'   => 'unpublished-post-with-a-slug',
+			)
+		);
+
+		$response = rest_get_server()->dispatch( $request );
+
+		$this->assertSame( 201, $response->get_status(), 'The post should have been created.' );
+
+		$data = $response->get_data();
+		$this->assertSame( 'unpublished-post-with-a-slug', $data['slug'], 'The requested slug should have been used.' );
+		$this->assertSame( $status, get_post( $data['id'] )->post_status, 'The requested status should have been saved.' );
+	}
+
+	/**
+	 * Data provider.
+	 *
+	 * @return array[] Test parameters.
+	 */
+	public function data_create_item_with_slug_and_unpublished_status() {
+		return array(
+			'draft status'   => array( 'draft' ),
+			'pending status' => array( 'pending' ),
+		);
+	}
+
 	public function revoke_assign_term( $caps, $cap, $user_id, $args ) {
 		if ( 'assign_term' === $cap && isset( $args[0] ) && $this->forbidden_cat === $args[0] ) {
 			$caps = array( 'do_not_allow' );
