Make WordPress Core

Changeset 59919


Ignore:
Timestamp:
03/03/2025 09:43:44 PM (14 months ago)
Author:
peterwilsoncc
Message:

Query: Ensure secondary loops populate the full global post.

Modifies WP_Query::the_post() to ensure the entire global post object is populated regardless of the fields parameter initially set by the developer.

In secondary loops, this ensures that get_the_content() and other getter functions operate as documented when called without a post ID and return the appropriate data for the global post object.

This introduces consistency when starting the loop and the fields parameter is set to id=>parent to the behaviour when set to either all or ids.

There is no change to the WP_Query::$posts parameter nor when a query is made without starting the secondary loop, ie without calling WP_Query::the_post().

Props juzar, mukesh27, oglekler, peterwilsoncc, sirlouen, joemcgill.
Fixes #56992.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r59796 r59919  
    37393739
    37403740        if ( ! $this->in_the_loop ) {
    3741             // Only prime the post cache for queries limited to the ID field.
    3742             $post_ids = array_filter( $this->posts, 'is_numeric' );
    3743             // Exclude any falsey values, such as 0.
    3744             $post_ids = array_filter( $post_ids );
     3741            // Get post IDs to prime incomplete post objects.
     3742            $post_ids = array_reduce(
     3743                $this->posts,
     3744                function ( $carry, $post ) {
     3745                    if ( is_numeric( $post ) && $post > 0 ) {
     3746                        // Query for post ID.
     3747                        $carry[] = $post;
     3748                    }
     3749
     3750                    if ( is_object( $post ) && isset( $post->ID ) ) {
     3751                        // Query for object, either WP_Post or stdClass.
     3752                        $carry[] = $post->ID;
     3753                    }
     3754
     3755                    return $carry;
     3756                },
     3757                array()
     3758            );
    37453759            if ( $post_ids ) {
    37463760                _prime_post_caches( $post_ids, $this->query_vars['update_post_term_cache'], $this->query_vars['update_post_meta_cache'] );
    37473761            }
    3748             $post_objects = array_map( 'get_post', $this->posts );
     3762            $post_objects = array_map( 'get_post', $post_ids );
    37493763            update_post_author_caches( $post_objects );
    37503764        }
     
    37653779
    37663780        $post = $this->next_post();
     3781
     3782        // Get the post ID.
     3783        if ( is_object( $post ) ) {
     3784            $global_post_id = $post->ID;
     3785        } else {
     3786            $global_post_id = $post;
     3787        }
     3788
     3789        // Ensure the global $post is the full post object.
     3790        $post = get_post( $global_post_id );
    37673791        $this->setup_postdata( $post );
    37683792    }
  • trunk/tests/phpunit/tests/query/cacheResults.php

    r59766 r59919  
    19261926     * @since 6.1.1
    19271927     * @ticket 56948
     1928     * @ticket 56992
    19281929     *
    19291930     * @covers WP_Query::the_post
     
    19791980    public function data_author_cache_warmed_by_the_loop() {
    19801981        return array(
    1981             'fields: empty' => array( '' ),
    1982             'fields: all'   => array( 'all' ),
    1983             'fields: ids'   => array( 'ids' ),
    1984             /*
    1985              * `id=>parent` is untested pending the resolution of an existing bug.
    1986              * See https://core.trac.wordpress.org/ticket/56992
    1987              */
     1982            'fields: empty'      => array( '' ),
     1983            'fields: all'        => array( 'all' ),
     1984            'fields: ids'        => array( 'ids' ),
     1985            'fields: id=>parent' => array( 'id=>parent' ),
    19881986        );
    19891987    }
Note: See TracChangeset for help on using the changeset viewer.