diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index a52b5a484e841..3fcb942aa782a 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3191,7 +3191,7 @@ public function get_posts() { return $this->posts; } elseif ( 'id=>parent' === $q['fields'] ) { - _prime_post_parents_caches( $post_ids ); + _prime_post_parent_id_caches( $post_ids ); /** @var int[] */ $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 99116470d59ef..6efc2126fa500 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -547,6 +547,7 @@ add_action( 'admin_menu', '_add_post_type_submenus' ); add_action( 'before_delete_post', '_reset_front_page_settings_for_post' ); add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' ); +add_action( 'save_post', '_set_post_parent_cache', 10, 2 ); add_action( 'change_locale', 'create_initial_post_types' ); // Post Formats. diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 1f2c65e1580e4..d51b2cb6f3692 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3559,6 +3559,22 @@ function _reset_front_page_settings_for_post( $post_id ) { unstick_post( $post->ID ); } +/** + * Set or update the post parent cache for a specific post. + * + * This function sets or updates the post parent cache for a given post ID. + * The post parent cache is used to store the parent ID of a post for quick retrieval. + * + * @since 6.4.0 + * @access private + * + * @param int $post_id The ID of the post for which to set or update the parent cache. + * @param WP_Post $post The post object representing the post. + */ +function _set_post_parent_cache( $post_id, $post ) { + wp_cache_set( $post_id, $post->post_parent, 'post_parent' ); +} + /** * Moves a post or page to the Trash * @@ -7805,7 +7821,7 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache * * @param int[] $ids ID list. */ -function _prime_post_parents_caches( array $ids ) { +function _prime_post_parent_id_caches( array $ids ) { global $wpdb; $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' ); diff --git a/tests/phpunit/tests/post/primePostParentsCaches.php b/tests/phpunit/tests/post/primePostParentIdCaches.php similarity index 72% rename from tests/phpunit/tests/post/primePostParentsCaches.php rename to tests/phpunit/tests/post/primePostParentIdCaches.php index a430450aabcf7..e9d0a65fb0f06 100644 --- a/tests/phpunit/tests/post/primePostParentsCaches.php +++ b/tests/phpunit/tests/post/primePostParentIdCaches.php @@ -1,19 +1,19 @@ assertSame( 1, $num_queries, 'Unexpected number of queries.' ); @@ -48,9 +48,9 @@ public function test_prime_post_parents_caches() { /** * @ticket 59188 */ - public function test_prime_post_parents_caches_multiple() { + public function test_prime_post_parent_id_caches_multiple() { $before_num_queries = get_num_queries(); - _prime_post_parents_caches( self::$posts ); + _prime_post_parent_id_caches( self::$posts ); $num_queries = get_num_queries() - $before_num_queries; $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' ); @@ -60,10 +60,10 @@ public function test_prime_post_parents_caches_multiple() { /** * @ticket 59188 */ - public function test_prime_post_parents_caches_multiple_runs() { - _prime_post_parents_caches( self::$posts ); + public function test_prime_post_parent_id_caches_multiple_runs() { + _prime_post_parent_id_caches( self::$posts ); $before_num_queries = get_num_queries(); - _prime_post_parents_caches( self::$posts ); + _prime_post_parent_id_caches( self::$posts ); $num_queries = get_num_queries() - $before_num_queries; $this->assertSame( 0, $num_queries, 'Unexpected number of queries.' ); @@ -72,15 +72,16 @@ public function test_prime_post_parents_caches_multiple_runs() { /** * @ticket 59188 */ - public function test_prime_post_parents_caches_update() { - $page_id = self::factory()->post->create( + public function test_prime_post_parent_id_caches_update() { + $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => self::$posts[0], ) ); + clean_post_cache( $page_id ); $before_num_queries = get_num_queries(); - _prime_post_parents_caches( array( $page_id ) ); + _prime_post_parent_id_caches( array( $page_id ) ); $num_queries = get_num_queries() - $before_num_queries; $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' ); @@ -94,30 +95,31 @@ public function test_prime_post_parents_caches_update() { ); $before_num_queries = get_num_queries(); - _prime_post_parents_caches( array( $page_id ) ); + _prime_post_parent_id_caches( array( $page_id ) ); $num_queries = get_num_queries() - $before_num_queries; - $this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' ); + $this->assertSame( 0, $num_queries, 'Unexpected number of queries on second run' ); $this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 1 as parent' ); } /** * @ticket 59188 */ - public function test_prime_post_parents_caches_delete() { - $parent_page_id = self::factory()->post->create( + public function test_prime_post_parent_id_caches_delete() { + $parent_page_id = self::factory()->post->create( array( 'post_type' => 'page', ) ); - $page_id = self::factory()->post->create( + $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $parent_page_id, ) ); + clean_post_cache( $page_id ); $before_num_queries = get_num_queries(); - _prime_post_parents_caches( array( $page_id ) ); + _prime_post_parent_id_caches( array( $page_id ) ); $num_queries = get_num_queries() - $before_num_queries; $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );