From 824e72c935fe5853c95b54f197d387e27867041e Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 4 Dec 2026 10:53:06 +0100 Subject: [PATCH 1/3] Tag the new homepage, not the old one, when page_on_front changes update_option_page_on_front gets the old value first, so the "homepage" page-type went to the previous homepage and was removed from the new one. Saving that previous page later made it the static homepage again (post_updated sets page_on_front for pages with that type). Register the hook with both arguments, tag the new value, and tag nothing when page_on_front is set to 0 (it tagged object 0 before). Co-Authored-By: Claude Opus 5.5 --- classes/class-page-types.php | 18 ++++++++++++----- tests/phpunit/test-class-page-types.php | 27 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/classes/class-page-types.php b/classes/class-page-types.php index fff857131..1fb7b047e 100644 --- a/classes/class-page-types.php +++ b/classes/class-page-types.php @@ -35,7 +35,7 @@ public function __construct() { \add_action( 'init', [ $this, 'maybe_update_terms' ] ); // Add hook when updating the `page_on_front` option. - \add_action( 'update_option_page_on_front', [ $this, 'update_option_page_on_front' ] ); + \add_action( 'update_option_page_on_front', [ $this, 'update_option_page_on_front' ], 10, 2 ); // Add activity when a post is added or updated. \add_action( 'post_updated', [ $this, 'post_updated' ], 10, 2 ); @@ -366,13 +366,20 @@ public function get_taxonomy_name() { } /** - * Update the `page_on_front` option. + * Move the homepage page-type to the new `page_on_front` page. * - * @param int $page_id The page ID. + * WordPress passes the old value first: tagging that one would mark the + * previous homepage, and post_updated() would then make it the homepage + * again the next time it is saved. + * + * @param mixed $old_value The previous page ID (unused). + * @param mixed $page_id The new page ID. * * @return void */ - public function update_option_page_on_front( $page_id ) { + public function update_option_page_on_front( $old_value, $page_id ) { + $page_id = (int) $page_id; + // Get the posts for the homepage. $posts = $this->get_posts_by_type( 'page', 'homepage' ); $term = \get_term_by( 'slug', 'homepage', self::TAXONOMY_NAME ); @@ -392,7 +399,8 @@ public function update_option_page_on_front( $page_id ) { \wp_remove_object_terms( $post->ID, $term->term_id, self::TAXONOMY_NAME ); } - if ( ! $same_page ) { + // No page (page_on_front set to 0): nothing to tag. + if ( ! $same_page && $page_id > 0 ) { // Add the term to the new homepage. \wp_set_object_terms( $page_id, $term->term_id, self::TAXONOMY_NAME ); self::assign_child_pages( $page_id, $term->term_id ); diff --git a/tests/phpunit/test-class-page-types.php b/tests/phpunit/test-class-page-types.php index e55eef843..b19a482c1 100644 --- a/tests/phpunit/test-class-page-types.php +++ b/tests/phpunit/test-class-page-types.php @@ -151,6 +151,33 @@ public function test_get_default_page_type() { * @return void */ public function test_update_option_page_on_front() { + $taxonomy = \progress_planner()->get_page_types()::TAXONOMY_NAME; + $first = self::factory()->post->create( [ 'post_type' => 'page' ] ); + $second = self::factory()->post->create( [ 'post_type' => 'page' ] ); + + \update_option( 'show_on_front', 'page' ); + \update_option( 'page_on_front', $first ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $first ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $second ) ); + + // The new homepage gets the page-type, not the previous one. + \update_option( 'page_on_front', $second ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $second ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $first ) ); + + // Saving the previous homepage must not make it the homepage again. + \wp_update_post( + [ + 'ID' => $first, + 'post_title' => 'Saved again', + ] + ); + $this->assertSame( $second, (int) \get_option( 'page_on_front' ) ); + + // No homepage: the page-type is removed and nothing else is tagged. + \update_option( 'page_on_front', 0 ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $second ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, 0 ) ); } /** From 19aafc16acccfc6e654819c8ed35ca27948b15b3 Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 4 Dec 2026 11:16:37 +0100 Subject: [PATCH 2/3] Untag every page with the homepage type when page_on_front changes get_posts_by_type() caches its result for the request, so a second page_on_front change in the same request worked from a stale list and left the previous homepage tagged (the new test caught this). Use get_objects_in_term() instead: it is uncached and also finds drafts and leftover objects that the old hook tagged by mistake. Co-Authored-By: Claude Opus 5.5 --- classes/class-page-types.php | 25 +++++++++++-------------- tests/phpunit/test-class-page-types.php | 14 +++++++++++++- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/classes/class-page-types.php b/classes/class-page-types.php index 1fb7b047e..24314c288 100644 --- a/classes/class-page-types.php +++ b/classes/class-page-types.php @@ -379,29 +379,26 @@ public function get_taxonomy_name() { */ public function update_option_page_on_front( $old_value, $page_id ) { $page_id = (int) $page_id; - - // Get the posts for the homepage. - $posts = $this->get_posts_by_type( 'page', 'homepage' ); - $term = \get_term_by( 'slug', 'homepage', self::TAXONOMY_NAME ); + $term = \get_term_by( 'slug', 'homepage', self::TAXONOMY_NAME ); if ( ! $term ) { return; } - $same_page = false; - foreach ( $posts as $post ) { - if ( (int) $post->ID === (int) $page_id ) { - $same_page = true; - continue; - } + // Everything that has the homepage page-type, in any status. Not + // get_posts_by_type(): its per-request cache would miss a page tagged + // earlier in the same request. + $tagged = \get_objects_in_term( $term->term_id, self::TAXONOMY_NAME ); + $tagged = \is_array( $tagged ) ? \array_map( 'intval', $tagged ) : []; - // Remove the term from the post. - \wp_remove_object_terms( $post->ID, $term->term_id, self::TAXONOMY_NAME ); + foreach ( $tagged as $object_id ) { + if ( $object_id !== $page_id ) { + \wp_remove_object_terms( $object_id, $term->term_id, self::TAXONOMY_NAME ); + } } // No page (page_on_front set to 0): nothing to tag. - if ( ! $same_page && $page_id > 0 ) { - // Add the term to the new homepage. + if ( $page_id > 0 && ! \in_array( $page_id, $tagged, true ) ) { \wp_set_object_terms( $page_id, $term->term_id, self::TAXONOMY_NAME ); self::assign_child_pages( $page_id, $term->term_id ); } diff --git a/tests/phpunit/test-class-page-types.php b/tests/phpunit/test-class-page-types.php index b19a482c1..957f0c19d 100644 --- a/tests/phpunit/test-class-page-types.php +++ b/tests/phpunit/test-class-page-types.php @@ -174,9 +174,21 @@ public function test_update_option_page_on_front() { ); $this->assertSame( $second, (int) \get_option( 'page_on_front' ) ); + // A draft that still has the page-type (from before this fix) loses it too. + $draft = self::factory()->post->create( + [ + 'post_type' => 'page', + 'post_status' => 'draft', + ] + ); + \wp_set_object_terms( $draft, 'homepage', $taxonomy ); + \update_option( 'page_on_front', $first ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $draft ) ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $first ) ); + // No homepage: the page-type is removed and nothing else is tagged. \update_option( 'page_on_front', 0 ); - $this->assertFalse( \has_term( 'homepage', $taxonomy, $second ) ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $first ) ); $this->assertFalse( \has_term( 'homepage', $taxonomy, 0 ) ); } From ec9bfeb7c96020a1274020246cd239427a6743ec Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Fri, 4 Dec 2026 11:16:38 +0100 Subject: [PATCH 3/3] Remove the homepage type when the site switches to latest posts WordPress keeps page_on_front when "Your latest posts" is chosen, so the former homepage kept the homepage page-type, and saving it later made post_updated() switch the site back to a static homepage. Untag on the switch away from a static homepage, and tag page_on_front again on the switch back. Co-Authored-By: Claude Opus 5.5 --- classes/class-page-types.php | 24 ++++++++++++++++++++ tests/phpunit/test-class-page-types.php | 30 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/classes/class-page-types.php b/classes/class-page-types.php index 24314c288..afee7f684 100644 --- a/classes/class-page-types.php +++ b/classes/class-page-types.php @@ -36,6 +36,7 @@ public function __construct() { // Add hook when updating the `page_on_front` option. \add_action( 'update_option_page_on_front', [ $this, 'update_option_page_on_front' ], 10, 2 ); + \add_action( 'update_option_show_on_front', [ $this, 'update_option_show_on_front' ], 10, 2 ); // Add activity when a post is added or updated. \add_action( 'post_updated', [ $this, 'post_updated' ], 10, 2 ); @@ -404,6 +405,29 @@ public function update_option_page_on_front( $old_value, $page_id ) { } } + /** + * Remove the homepage page-type when the site stops using a static homepage. + * + * WordPress keeps page_on_front when "Your latest posts" is chosen, so + * update_option_page_on_front() does not run. Without this, the former + * homepage keeps the page-type, and post_updated() switches the site back + * to a static homepage the next time that page is saved. + * + * @param mixed $old_value The previous value (unused). + * @param mixed $value The new value. + * + * @return void + */ + public function update_option_show_on_front( $old_value, $value ) { + if ( 'page' === $value ) { + // Back to a static homepage: tag page_on_front again. + $this->update_option_page_on_front( null, \get_option( 'page_on_front' ) ); + return; + } + + $this->update_option_page_on_front( null, 0 ); + } + /** * Post updated. * diff --git a/tests/phpunit/test-class-page-types.php b/tests/phpunit/test-class-page-types.php index 957f0c19d..1f4331837 100644 --- a/tests/phpunit/test-class-page-types.php +++ b/tests/phpunit/test-class-page-types.php @@ -192,6 +192,36 @@ public function test_update_option_page_on_front() { $this->assertFalse( \has_term( 'homepage', $taxonomy, 0 ) ); } + /** + * Switching to "Your latest posts" removes the homepage page-type, and + * switching back restores it on page_on_front. + * + * @return void + */ + public function test_update_option_show_on_front() { + $taxonomy = \progress_planner()->get_page_types()::TAXONOMY_NAME; + $page = self::factory()->post->create( [ 'post_type' => 'page' ] ); + + \update_option( 'show_on_front', 'page' ); + \update_option( 'page_on_front', $page ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $page ) ); + + \update_option( 'show_on_front', 'posts' ); + $this->assertFalse( \has_term( 'homepage', $taxonomy, $page ) ); + + // Saving the former homepage keeps the latest posts on the front page. + \wp_update_post( + [ + 'ID' => $page, + 'post_title' => 'Saved again', + ] + ); + $this->assertSame( 'posts', \get_option( 'show_on_front' ) ); + + \update_option( 'show_on_front', 'page' ); + $this->assertTrue( \has_term( 'homepage', $taxonomy, $page ) ); + } + /** * Test post_updated. *