Changeset 56763
- Timestamp:
- 10/03/2023 02:59:22 PM (12 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r56656 r56763 3180 3180 3181 3181 if ( $cached_results ) { 3182 if ( 'ids' === $q['fields'] ) { 3183 /** @var int[] */ 3184 $this->posts = array_map( 'intval', $cached_results['posts'] ); 3185 } else { 3186 _prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] ); 3187 /** @var WP_Post[] */ 3188 $this->posts = array_map( 'get_post', $cached_results['posts'] ); 3189 } 3190 3191 $this->post_count = count( $this->posts ); 3182 /** @var int[] */ 3183 $post_ids = array_map( 'intval', $cached_results['posts'] ); 3184 3185 $this->post_count = count( $post_ids ); 3192 3186 $this->found_posts = $cached_results['found_posts']; 3193 3187 $this->max_num_pages = $cached_results['max_num_pages']; 3194 3188 3195 3189 if ( 'ids' === $q['fields'] ) { 3190 $this->posts = $post_ids; 3191 3196 3192 return $this->posts; 3197 3193 } elseif ( 'id=>parent' === $q['fields'] ) { 3194 _prime_post_parents_caches( $post_ids ); 3195 3198 3196 /** @var int[] */ 3199 $post_parents = array();3200 3201 foreach ( $ this->posts as $key => $post ) {3197 $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); 3198 3199 foreach ( $post_parents as $id => $post_parent ) { 3202 3200 $obj = new stdClass(); 3203 $obj->ID = (int) $post->ID; 3204 $obj->post_parent = (int) $post->post_parent; 3205 3206 $this->posts[ $key ] = $obj; 3207 3208 $post_parents[ $obj->ID ] = $obj->post_parent; 3201 $obj->ID = (int) $id; 3202 $obj->post_parent = (int) $post_parent; 3203 3204 $this->posts[] = $obj; 3209 3205 } 3210 3206 3211 3207 return $post_parents; 3208 } else { 3209 _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); 3210 /** @var WP_Post[] */ 3211 $this->posts = array_map( 'get_post', $post_ids ); 3212 3212 } 3213 3213 } … … 3257 3257 $post_ids[] = (int) $post->ID; 3258 3258 } 3259 // Prime post parent caches, so that on second run, there is not another database query. 3260 wp_cache_add_multiple( $post_parents, 'post_parent' ); 3259 3261 3260 3262 if ( $q['cache_results'] && $id_query_is_cacheable ) { -
trunk/src/wp-includes/functions.php
r56753 r56763 7170 7170 7171 7171 foreach ( $cache_values as $id => $value ) { 7172 if ( !$value ) {7172 if ( false === $value ) { 7173 7173 $non_cached_ids[] = (int) $id; 7174 7174 } -
trunk/src/wp-includes/post.php
r56711 r56763 7263 7263 wp_cache_delete( $post->ID, 'posts' ); 7264 7264 wp_cache_delete( $post->ID, 'post_meta' ); 7265 wp_cache_delete( $post->ID, 'post_parent' ); 7265 7266 7266 7267 clean_object_term_cache( $post->ID, $post->post_type ); … … 7797 7798 7798 7799 /** 7800 * Prime post parent caches. 7801 * 7802 * @global wpdb $wpdb WordPress database abstraction object. 7803 * 7804 * @param int[] $ids ID list. 7805 */ 7806 function _prime_post_parents_caches( array $ids ) { 7807 global $wpdb; 7808 7809 $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' ); 7810 if ( ! empty( $non_cached_ids ) ) { 7811 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); 7812 7813 if ( $fresh_posts ) { 7814 $post_parent_data = array(); 7815 foreach ( $fresh_posts as $fresh_post ) { 7816 $post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent; 7817 } 7818 7819 wp_cache_add_multiple( $post_parent_data, 'post_parent' ); 7820 } 7821 } 7822 } 7823 7824 /** 7799 7825 * Adds a suffix if any trashed posts have a given slug. 7800 7826 * -
trunk/tests/phpunit/tests/query/cacheResults.php
r56656 r56763 711 711 $queries_after = get_num_queries(); 712 712 713 $this->assertSame( $queries_before, $queries_after);713 $this->assertSame( 1, $queries_after - $queries_before ); 714 714 $this->assertCount( 5, $query1->posts ); 715 715 $this->assertCount( 5, $query2->posts ); … … 725 725 */ 726 726 $this->assertNotEquals( $query1->posts, $query2->posts ); 727 } 728 729 730 /** 731 * @ticket 59188 732 */ 733 public function test_query_cache_unprimed_parents() { 734 $args = array( 735 'cache_results' => true, 736 'fields' => 'id=>parent', 737 ); 738 $query1 = new WP_Query(); 739 $query1->query( $args ); 740 741 $post_ids = wp_list_pluck( $query1->posts, 'ID' ); 742 wp_cache_delete_multiple( $post_ids, 'post_parent' ); 743 744 $queries_before = get_num_queries(); 745 $query2 = new WP_Query(); 746 $query2->query( $args ); 747 $queries_after = get_num_queries(); 748 749 $this->assertSame( 1, $queries_after - $queries_before, 'There should be only one query to prime parents' ); 750 $this->assertCount( 5, $query1->posts, 'There should be only 5 posts returned on first query' ); 751 $this->assertCount( 5, $query2->posts, 'There should be only 5 posts returned on second query' ); 752 $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' ); 753 } 754 755 /** 756 * @ticket 59188 757 */ 758 public function test_query_cache_update_parent() { 759 $page_id = self::factory()->post->create( 760 array( 761 'post_type' => 'page', 762 'post_parent' => self::$pages[0], 763 ) 764 ); 765 $args = array( 766 'cache_results' => true, 767 'post_type' => 'page', 768 'fields' => 'id=>parent', 769 'post__in' => array( 770 $page_id, 771 ), 772 ); 773 $query1 = new WP_Query(); 774 $query1->query( $args ); 775 776 wp_update_post( 777 array( 778 'ID' => $page_id, 779 'post_parent' => self::$pages[1], 780 ) 781 ); 782 783 $queries_before = get_num_queries(); 784 $query2 = new WP_Query(); 785 $query2->query( $args ); 786 $queries_after = get_num_queries(); 787 788 $this->assertSame( self::$pages[0], $query1->posts[0]->post_parent, 'Check post parent on first query' ); 789 $this->assertSame( self::$pages[1], $query2->posts[0]->post_parent, 'Check post parent on second query' ); 790 $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' ); 791 $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' ); 792 } 793 794 /** 795 * @ticket 59188 796 */ 797 public function test_query_cache_delete_parent() { 798 $parent_page_id = self::factory()->post->create( 799 array( 800 'post_type' => 'page', 801 ) 802 ); 803 $page_id = self::factory()->post->create( 804 array( 805 'post_type' => 'page', 806 'post_parent' => $parent_page_id, 807 ) 808 ); 809 $args = array( 810 'cache_results' => true, 811 'post_type' => 'page', 812 'fields' => 'id=>parent', 813 'post__in' => array( 814 $page_id, 815 ), 816 ); 817 $query1 = new WP_Query(); 818 $query1->query( $args ); 819 820 wp_delete_post( $parent_page_id, true ); 821 822 $queries_before = get_num_queries(); 823 $query2 = new WP_Query(); 824 $query2->query( $args ); 825 $queries_after = get_num_queries(); 826 827 $this->assertSame( $parent_page_id, $query1->posts[0]->post_parent, 'Check post parent on first query' ); 828 $this->assertSame( 0, $query2->posts[0]->post_parent, 'Check post parent on second query' ); 829 $this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' ); 830 $this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' ); 727 831 } 728 832
Note: See TracChangeset
for help on using the changeset viewer.