From 680c4cace0c488af763e83ad95aa5c0115248c61 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Thu, 17 Sep 2026 22:43:31 +0600 Subject: [PATCH] fix: copy the sticky state when duplicating Sticky posts are tracked in the sticky_posts option instead of post fields, so neither duplication path carried the setting over. Now the duplicate is marked sticky when the original is a sticky post, in both the namespaced duplicator (Rewrite & Republish) and the legacy function used by Clone and New Draft. Fixes #371 --- admin-functions.php | 5 ++++ src/post-duplicator.php | 5 ++++ tests/WP/Legacy_Duplication_Test.php | 33 +++++++++++++++++++++++++ tests/WP/Post_Republisher_Test.php | 36 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) diff --git a/admin-functions.php b/admin-functions.php index 26acf31ae..f3d74e70d 100644 --- a/admin-functions.php +++ b/admin-functions.php @@ -763,6 +763,11 @@ function duplicate_post_create_duplicate( $post, $status = '', $parent_id = '' ) delete_post_meta( $new_post_id, '_dp_original' ); add_post_meta( $new_post_id, '_dp_original', $post->ID ); + + // Sticky is only a feature of the post post type. + if ( $post->post_type === 'post' && is_sticky( $post->ID ) ) { + stick_post( $new_post_id ); + } } /** diff --git a/src/post-duplicator.php b/src/post-duplicator.php index e23a61e1e..78b86c02a 100644 --- a/src/post-duplicator.php +++ b/src/post-duplicator.php @@ -119,6 +119,11 @@ public function create_duplicate( WP_Post $post, array $options = [] ) { if ( ! \is_wp_error( $new_post_id ) ) { \delete_post_meta( $new_post_id, '_dp_original' ); \add_post_meta( $new_post_id, '_dp_original', $post->ID ); + + // Sticky is only a feature of the post post type. + if ( $post->post_type === 'post' && \is_sticky( $post->ID ) ) { + \stick_post( $new_post_id ); + } } return $new_post_id; diff --git a/tests/WP/Legacy_Duplication_Test.php b/tests/WP/Legacy_Duplication_Test.php index e78a0cc4b..36999f613 100644 --- a/tests/WP/Legacy_Duplication_Test.php +++ b/tests/WP/Legacy_Duplication_Test.php @@ -690,6 +690,39 @@ public function test_clone_of_clone_points_to_direct_source() { $this->assertSame( $first_copy_id, (int) \get_post_meta( $second_copy_id, '_dp_original', true ) ); } + /** + * Tests that the sticky setting is copied to the duplicate. + * + * @covers ::duplicate_post_create_duplicate + * + * @return void + */ + public function test_copies_sticky_state() { + $original = $this->create_original_post(); + \stick_post( $original->ID ); + + $new_id = \duplicate_post_create_duplicate( $original ); + + $this->assertIsInt( $new_id ); + $this->assertTrue( \is_sticky( $new_id ) ); + } + + /** + * Tests that a non sticky post does not produce a sticky duplicate. + * + * @covers ::duplicate_post_create_duplicate + * + * @return void + */ + public function test_does_not_stick_duplicate_of_non_sticky_post() { + $original = $this->create_original_post(); + + $new_id = \duplicate_post_create_duplicate( $original ); + + $this->assertIsInt( $new_id ); + $this->assertFalse( \is_sticky( $new_id ) ); + } + /** * Tests that a disabled copystatus setting forces draft even when a status is passed. * diff --git a/tests/WP/Post_Republisher_Test.php b/tests/WP/Post_Republisher_Test.php index 67a3a8ae1..a31eb58f4 100644 --- a/tests/WP/Post_Republisher_Test.php +++ b/tests/WP/Post_Republisher_Test.php @@ -212,6 +212,42 @@ public function test_create_duplicate_for_rewrite_and_republish_copies_content() $this->assertSame( 'draft', $copy->post_status ); } + /** + * Tests that create_duplicate_for_rewrite_and_republish copies sticky state. + * + * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::create_duplicate_for_rewrite_and_republish + * @covers \Yoast\WP\Duplicate_Post\Post_Duplicator::create_duplicate + * + * @return void + */ + public function test_create_duplicate_for_rewrite_and_republish_copies_sticky_state() { + $original = $this->create_original_post(); + \stick_post( $original->ID ); + + $copy = $this->create_rewrite_and_republish_copy( $original ); + + $this->assertTrue( \is_sticky( $copy->ID ) ); + } + + /** + * Tests that republish does not clear the original post sticky state. + * + * @covers ::republish + * @covers ::republish_post_elements + * + * @return void + */ + public function test_republish_preserves_original_sticky_state() { + $original = $this->create_original_post(); + \stick_post( $original->ID ); + + $copy = $this->create_rewrite_and_republish_copy( $original ); + + $this->instance->republish( $copy, $original ); + + $this->assertTrue( \is_sticky( $original->ID ) ); + } + /** * Tests that create_duplicate_for_rewrite_and_republish copies taxonomies. *