Make WordPress Core


Ignore:
Timestamp:
11/10/2022 02:20:47 AM (4 years ago)
Author:
peterwilsoncc
Message:

Query: Prevent ID only queries erroring when starting the loop.

Ensure only full post objects are passed to update_post_author_caches() when called within WP_Query::the_post(). This prevents an error when starting the Loop for Queries initiated with a subset of fields or IDs only.

Props konyoldeath, dd32, lozula, TimothyBlynJacobs, spacedmonkey, mxbclang, peterwilsoncc.
Merges [54771] to the 6.1 branch.
Fixes #56948.

Location:
branches/6.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/tests/phpunit/tests/query/cacheResults.php

    r54692 r54784  
    12111211                );
    12121212        }
     1213
     1214        /**
     1215         * Ensure starting the loop warms the author cache.
     1216         *
     1217         * @since 6.1.1
     1218         * @ticket 56948
     1219         *
     1220         * @covers WP_Query::the_post
     1221         *
     1222         * @dataProvider data_author_cache_warmed_by_the_loop
     1223         *
     1224         * @param string $fields Query fields.
     1225         */
     1226        public function test_author_cache_warmed_by_the_loop( $fields ) {
     1227                // Update post author for the parent post.
     1228                self::factory()->post->update_object( self::$pages[0], array( 'post_author' => self::$author_id ) );
     1229
     1230                self::factory()->post->create(
     1231                        array(
     1232                                'post_author' => self::$author_id,
     1233                                'post_parent' => self::$pages[0],
     1234                                'post_type'   => 'page',
     1235                        )
     1236                );
     1237
     1238                $query_1 = new WP_Query(
     1239                        array(
     1240                                'post_type' => 'page',
     1241                                'fields'    => $fields,
     1242                                'author'    => self::$author_id,
     1243                        )
     1244                );
     1245
     1246                // Start the loop.
     1247                $start_loop_queries = get_num_queries();
     1248                $query_1->the_post();
     1249                $num_loop_queries = get_num_queries() - $start_loop_queries;
     1250                $this->assertSame( 2, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
     1251
     1252                $start_author_queries = get_num_queries();
     1253                get_user_by( 'ID', self::$author_id );
     1254                $num_author_queries = get_num_queries() - $start_author_queries;
     1255                $this->assertSame( 0, $num_author_queries, 'Author cache is not warmed by the loop.' );
     1256        }
     1257
     1258        /**
     1259         * Data provider for test_author_cache_warmed_by_the_loop
     1260         *
     1261         * @return array[]
     1262         */
     1263        public function data_author_cache_warmed_by_the_loop() {
     1264                return array(
     1265                        'fields: empty' => array( '' ),
     1266                        'fields: all'   => array( 'all' ),
     1267                        'fields: ids'   => array( 'ids' ),
     1268                        /*
     1269                         * `id=>parent` is untested pending the resolution of an existing bug.
     1270                         * See https://core.trac.wordpress.org/ticket/56992
     1271                         */
     1272                );
     1273        }
    12131274}
Note: See TracChangeset for help on using the changeset viewer.