diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index c0e5e1fc70..073b4aeddc 100644
--- a/src/wp-includes/post.php
+++ b/src/wp-includes/post.php
@@ -4375,6 +4375,14 @@ function wp_publish_post( $post ) {
 		return;
 	}
 
+	// A published 'post' requires at least one category.
+	if (
+		'post' === $post->post_type &&
+		empty( get_the_category( $post->ID ) )
+	) {
+		wp_set_post_categories( $post->ID, get_option( 'default_category' ) );
+	}
+
 	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
 
 	clean_post_cache( $post->ID );
diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
index bacf9b23c5..aa0414ebb1 100644
--- a/tests/phpunit/tests/post.php
+++ b/tests/phpunit/tests/post.php
@@ -675,6 +675,53 @@ function test_wp_publish_post_and_avoid_content_filtering() {
 		kses_remove_filters();
 	}
 
+	/**
+	 * Ensure wp_publish_post does not add default category in error.
+	 *
+	 * @ticket 51292
+	 */
+	function test_wp_publish_post_respects_current_categories() {
+		$post_id     = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
+		$category_id = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
+		wp_set_post_categories( $post_id, $category_id );
+		wp_publish_post( $post_id );
+
+		$post_categories = get_the_category( $post_id );
+		$this->assertCount( 1, $post_categories );
+		$this->assertSame( $category_id, $post_categories[0]->term_id );
+	}
+
+	/**
+	 * Ensure wp_publish_post adds default category.
+	 *
+	 * @ticket 51292
+	 */
+	function test_wp_publish_post_adds_default_category() {
+		$post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
+
+		wp_publish_post( $post_id );
+
+		$post_categories = get_the_category( $post_id );
+		$this->assertCount( 1, $post_categories );
+		$this->assertSame( (int) get_option( 'default_category' ), $post_categories[0]->term_id );
+	}
+
+	/**
+	 * Ensure wp_publish_post adds default category when tagged.
+	 *
+	 * @ticket 51292
+	 */
+	function test_wp_publish_post_adds_default_category_when_tagged() {
+		$post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
+		$tag_id  = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) );
+		wp_set_post_tags( $post_id, array( $tag_id ) );
+		wp_publish_post( $post_id );
+
+		$post_categories = get_the_category( $post_id );
+		$this->assertCount( 1, $post_categories );
+		$this->assertSame( (int) get_option( 'default_category' ), $post_categories[0]->term_id );
+	}
+
 	/**
 	 * @ticket 23708
 	 */
diff --git a/tests/phpunit/tests/term/termCounts.php b/tests/phpunit/tests/term/termCounts.php
new file mode 100644
index 0000000000..4836799333
--- /dev/null
+++ b/tests/phpunit/tests/term/termCounts.php
@@ -0,0 +1,321 @@
+<?php
+
+/**
+ * @group taxonomy
+ */
+class Tests_Term_termCount extends WP_UnitTestCase {
+
+	/**
+	 * Term ID for testing attachment counts.
+	 *
+	 * @var int
+	 */
+	public static $attachment_term;
+
+	public static function wpSetUpBeforeClass( $factory ) {
+		register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
+		self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) );
+	}
+
+	public function setUp() {
+		parent::setUp();
+
+		register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
+	}
+
+	/**
+	 * Term counts are not double incremented when post created.
+	 *
+	 * @dataProvider data_term_count_changes_for_post_statuses
+	 * @ticket 40351
+	 * @ticket 51292
+	 */
+	public function test_term_count_changes_for_post_statuses( $post_status, $change ) {
+		$term_count = get_term( get_option( 'default_category' ) )->count;
+		$post_id    = self::factory()->post->create( array( 'post_status' => $post_status ) );
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_changes_for_post_statuses.
+	 *
+	 * @return array[] {
+	 *     @type string $post_status New post status.
+	 *     @type int    $change      Expected change.
+	 * }
+	 */
+	function data_term_count_changes_for_post_statuses() {
+		return array(
+			// 0. Published post
+			array( 'publish', 1 ),
+			// 1. Auto draft
+			array( 'auto-draft', 0 ),
+			// 2. Draft
+			array( 'draft', 0 ),
+			// 3. Private post
+			array( 'private', 0 ),
+		);
+	}
+
+	/**
+	 * Term counts increments correctly when post status becomes published.
+	 *
+	 * @dataProvider data_term_counts_incremented_on_publish
+	 * @ticket 40351
+	 * @ticket 51292
+	 */
+	public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
+		$post_id    = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
+		$term_count = get_term( get_option( 'default_category' ) )->count;
+
+		wp_publish_post( $post_id );
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_changes_for_post_statuses.
+	 *
+	 * @return array[] {
+	 *     @type string $original_post_status Post status prior to change to publish.
+	 *     @type int    $change               Expected change upon publish.
+	 * }
+	 */
+	function data_term_counts_incremented_on_publish() {
+		return array(
+			// 0. Published post
+			array( 'publish', 0 ),
+			// 1. Auto draft
+			array( 'auto-draft', 1 ),
+			// 2. Draft
+			array( 'draft', 1 ),
+			// 3. Private post
+			array( 'private', 1 ),
+		);
+	}
+
+	/**
+	 * Test post status transition update term counts correctly.
+	 *
+	 * @dataProvider data_term_count_transitions_update_term_counts
+	 * @ticket 40351
+	 * @ticket 51292
+	 */
+	function test_term_count_transitions_update_term_counts( $original_post_status, $new_post_status, $change ) {
+		$post_id    = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
+		$term_count = get_term( get_option( 'default_category' ) )->count;
+
+		wp_update_post(
+			array(
+				'ID'          => $post_id,
+				'post_status' => $new_post_status,
+			)
+		);
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_transitions_update_term_counts.
+	 *
+	 * @return array[] {
+	 *     @type string $original_post_status Post status upon create.
+	 *     @type string $new_post_status      Post status after update.
+	 *     @type int    $change               Expected change upon publish.
+	 * }
+	 */
+	function data_term_count_transitions_update_term_counts() {
+		return array(
+			// 0. Draft -> published post
+			array( 'draft', 'publish', 1 ),
+			// 1. Auto draft -> published post
+			array( 'auto-draft', 'publish', 1 ),
+			// 2. Private -> published post
+			array( 'private', 'publish', 1 ),
+			// 3. Published -> published post
+			array( 'publish', 'publish', 0 ),
+
+			// 4. Draft -> private post
+			array( 'draft', 'private', 0 ),
+			// 5. Auto draft -> private post
+			array( 'auto-draft', 'private', 0 ),
+			// 6. Private -> private post
+			array( 'private', 'private', 0 ),
+			// 7. Published -> private post
+			array( 'publish', 'private', -1 ),
+
+			// 8. Draft -> draft post
+			array( 'draft', 'draft', 0 ),
+			// 9. Auto draft -> draft post
+			array( 'auto-draft', 'draft', 0 ),
+			// 10. Private -> draft post
+			array( 'private', 'draft', 0 ),
+			// 11. Published -> draft post
+			array( 'publish', 'draft', -1 ),
+		);
+	}
+
+	/**
+	 * Term counts are not double incremented when post created.
+	 *
+	 * @dataProvider data_term_count_changes_for_post_statuses_with_attachments
+	 * @ticket 40351
+	 */
+	public function test_term_count_changes_for_post_statuses_with_attachments( $post_status, $change ) {
+		$term_count = get_term( self::$attachment_term )->count;
+		$post_id    = self::factory()->post->create( array( 'post_status' => $post_status ) );
+		wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
+		$attachment_id = self::factory()->attachment->create_object(
+			array(
+				'file'        => 'image.jpg',
+				'post_parent' => $post_id,
+				'post_status' => 'inherit',
+			)
+		);
+		wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( self::$attachment_term )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
+	 *
+	 * @return array[] {
+	 *     @type string $post_status New post status.
+	 *     @type int    $change      Expected change.
+	 * }
+	 */
+	function data_term_count_changes_for_post_statuses_with_attachments() {
+		return array(
+			// 0. Published post
+			array( 'publish', 2 ),
+			// 1. Auto draft
+			array( 'auto-draft', 0 ),
+			// 2. Draft
+			array( 'draft', 0 ),
+			// 3. Private post
+			array( 'private', 0 ),
+		);
+	}
+
+	/**
+	 * Term counts increments correctly when post status becomes published.
+	 *
+	 * @dataProvider data_term_counts_incremented_on_publish_with_attachments
+	 * @ticket 40351
+	 */
+	public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
+		$post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
+		wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
+		$attachment_id = self::factory()->attachment->create_object(
+			array(
+				'file'        => 'image.jpg',
+				'post_parent' => $post_id,
+				'post_status' => 'inherit',
+			)
+		);
+		wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
+		$term_count = get_term( self::$attachment_term )->count;
+
+		wp_publish_post( $post_id );
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( self::$attachment_term )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
+	 *
+	 * @return array[] {
+	 *     @type string $original_post_status Post status prior to change to publish.
+	 *     @type int    $change               Expected change upon publish.
+	 * }
+	 */
+	function data_term_counts_incremented_on_publish_with_attachments() {
+		return array(
+			// 0. Published post
+			array( 'publish', 0 ),
+			// 1. Auto draft
+			array( 'auto-draft', 2 ),
+			// 2. Draft
+			array( 'draft', 2 ),
+			// 3. Private post
+			array( 'private', 2 ),
+		);
+	}
+
+	/**
+	 * Test post status transition update term counts correctly.
+	 *
+	 * @dataProvider data_term_count_transitions_update_term_counts_with_attachments
+	 * @ticket 40351
+	 */
+	function test_term_count_transitions_update_term_counts_with_attachments( $original_post_status, $new_post_status, $change ) {
+		$post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
+		wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
+		$attachment_id = self::factory()->attachment->create_object(
+			array(
+				'file'        => 'image.jpg',
+				'post_parent' => $post_id,
+				'post_status' => 'inherit',
+			)
+		);
+		wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
+		$term_count = get_term( self::$attachment_term )->count;
+
+		wp_update_post(
+			array(
+				'ID'          => $post_id,
+				'post_status' => $new_post_status,
+			)
+		);
+
+		$expected = $term_count + $change;
+		$this->assertSame( $expected, get_term( self::$attachment_term )->count );
+	}
+
+	/**
+	 * Data provider for test_term_count_transitions_update_term_counts_with_attachments.
+	 *
+	 * @return array[] {
+	 *     @type string $original_post_status Post status upon create.
+	 *     @type string $new_post_status      Post status after update.
+	 *     @type int    $change               Expected change upon publish.
+	 * }
+	 */
+	function data_term_count_transitions_update_term_counts_with_attachments() {
+		return array(
+			// 0. Draft -> published post
+			array( 'draft', 'publish', 2 ),
+			// 1. Auto draft -> published post
+			array( 'auto-draft', 'publish', 2 ),
+			// 2. Private -> published post
+			array( 'private', 'publish', 2 ),
+			// 3. Published -> published post
+			array( 'publish', 'publish', 0 ),
+
+			// 4. Draft -> private post
+			array( 'draft', 'private', 0 ),
+			// 5. Auto draft -> private post
+			array( 'auto-draft', 'private', 0 ),
+			// 6. Private -> private post
+			array( 'private', 'private', 0 ),
+			// 7. Published -> private post
+			array( 'publish', 'private', -2 ),
+
+			// 8. Draft -> draft post
+			array( 'draft', 'draft', 0 ),
+			// 9. Auto draft -> draft post
+			array( 'auto-draft', 'draft', 0 ),
+			// 10. Private -> draft post
+			array( 'private', 'draft', 0 ),
+			// 11. Published -> draft post
+			array( 'publish', 'draft', -2 ),
+		);
+	}
+}
