Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect global post object when field is fields => 'id=>parent for WP_Query. #7985

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3662,14 +3662,24 @@ private function set_found_posts( $q, $limits ) {
*
* @since 1.5.0
*
* @return WP_Post Next post.
* @return WP_Post|int Next post.
*/
public function next_post() {

++$this->current_post;

/** @var WP_Post */
$this->post = $this->posts[ $this->current_post ];
if ( $this->posts[ $this->current_post ] instanceof WP_Post ) {
$this->post = $this->posts[ $this->current_post ];
} else {
$current_item = $this->posts[ $this->current_post ];

if ( is_object( $current_item ) && property_exists( $current_item, 'ID' ) ) {
$this->post = $current_item->ID;
} else {
$this->post = $current_item;
}
}

return $this->post;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,38 @@ static function () {
$this->assertSame( '/* posts_join_paged */', $posts_clauses['join'] );
}

/**
* @ticket 56992
*/
public function test_the_loop_when_querying_post_parents_only() {
$parent = self::factory()->post->create(
array(
'post_type' => 'page',
)
);

$child = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $parent,
)
);

$query = new WP_Query(
array(
'fields' => 'id=>parent',
'post_type' => 'page',
'page_id' => $child,
)
);

$query->the_post();
$global_post = get_post( null, ARRAY_A );
$specific_post = get_post( $child, ARRAY_A );

$this->assertEqualSetsWithIndex( $global_post, $specific_post );
}

/**
* Tests that the `posts_clauses_request` filter receives an array of clauses
* with the other `posts_*_request` filters applied, e.g. `posts_join_request`.
Expand Down
Loading