Index: src/wp-admin/includes/ajax-actions.php
===================================================================
--- src/wp-admin/includes/ajax-actions.php	(revision 36598)
+++ src/wp-admin/includes/ajax-actions.php	(working copy)
@@ -1583,6 +1583,7 @@
 	$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
 	$title = isset($_POST['new_title'])? $_POST['new_title'] : '';
 	$slug = isset($_POST['new_slug'])? $_POST['new_slug'] : null;
+
 	wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
 }
 
Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 36598)
+++ src/wp-includes/post.php	(working copy)
@@ -3241,6 +3241,25 @@
 	 */
 	$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
 
+	// If the post is being untrashed and its slug was changed previously.
+	if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
+		$desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
+		delete_post_meta( $post_ID, '_wp_desired_post_slug' );
+		if ( $desired_post_slug ) {
+			$post_name = $desired_post_slug;
+		}
+	}
+
+	// If a trashed post has the desired slug, change it and let this post have it.
+	if ( $post_status != 'trash' && $post_name ) {
+		wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
+	}
+
+	// When trashing a post, change its slug to allow non-trashed posts to use it.
+	if ( 'trash' === $post_status ) {
+		$post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
+	}
+
 	$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
 
 	// Don't unslash.
@@ -6033,3 +6052,56 @@
 		update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
 	}
 }
+
+/**
+ * If any trashed posts have a given slug, add a suffix.
+ *
+ * Store its desired (i.e. current) slug so it can try to reclaim it
+ * if the post is untrashed.
+ *
+ * For internal use.
+ *
+ * @since 4.5.0
+ *
+ * @param string $post_name    Slug.
+ * @param string $post__not_in Post ID that should be ignored.
+ */
+function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
+	$trashed_posts_with_desired_slug = get_posts( array(
+		'name' => $post_name,
+		'post_status' => 'trash',
+		'post_type' => 'any',
+		'nopaging' => true,
+		'post__not_in' => array( $post_ID )
+	) );
+
+	if ( ! empty( $trashed_posts_with_desired_slug ) ) {
+		foreach ( $trashed_posts_with_desired_slug as $_post ) {
+			wp_add_trashed_suffix_to_post_name_for_post( $_post );
+		}
+	}
+}
+
+/**
+ * For a given post, add a trashed suffix.
+ *
+ * Store its desired (i.e. current) slug so it can try to reclaim it
+ * if the post is untrashed.
+ *
+ * For internal use.
+ *
+ * @since 4.5.0
+ *
+ * @param WP_Post $post The post.
+ */
+function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
+	global $wpdb;
+
+	$post = get_post( $post );
+	// Store the desired slug so it can be reapplied if it is untrashed.
+	add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
+	$post_name = _truncate_post_slug( $post->post_name, 189 ) . "-%trashed%";
+	$wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
+	clean_post_cache( $post->ID );
+	return $post_name;
+}
Index: tests/phpunit/tests/post/wpInsertPost.php
===================================================================
--- tests/phpunit/tests/post/wpInsertPost.php	(nonexistent)
+++ tests/phpunit/tests/post/wpInsertPost.php	(working copy)
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * @group post
+ */
+class Tests_WPInsertPost extends WP_UnitTestCase {
+
+	/**
+	 * @ticket 11863
+	 */
+	function test_trashed_post_slugs_should_move_for_non_trashed_posts() {
+		$trashed_about_page_id = self::factory()->post->create( array(
+			'post_type' => 'page',
+			'post_title' => 'About',
+			'post_status' => 'publish'
+		) );
+		wp_trash_post( $trashed_about_page_id );
+		$about_page_id = self::factory()->post->create( array(
+			'post_type' => 'page',
+			'post_title' => 'About',
+			'post_status' => 'publish'
+		) );
+		$this->assertEquals( 'about', get_post( $about_page_id )->post_name );
+		$this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
+	}
+
+	/**
+	 * @ticket 11863
+	 */
+	function test_trashed_post_slugs_that_were_moved_should_be_reassigned_after_untrashing() {
+		$about_page_id = self::factory()->post->create( array(
+			'post_type' => 'page',
+			'post_title' => 'About',
+			'post_status' => 'publish'
+		) );
+		wp_trash_post( $about_page_id );
+
+		$another_about_page_id = self::factory()->post->create( array(
+			'post_type' => 'page',
+			'post_title' => 'About',
+			'post_status' => 'publish'
+		) );
+		wp_trash_post( $another_about_page_id );
+
+		wp_untrash_post( $about_page_id );
+		$this->assertEquals( 'about', get_post( $about_page_id )->post_name );
+	}
+}
