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

Duplicate query in WP_Query related to sticky post. #5295

Closed
wants to merge 20 commits into from

Conversation

kt-12
Copy link
Member

@kt-12 kt-12 commented Sep 25, 2023

Trac ticket: https://core.trac.wordpress.org/ticket/59442


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@spacedmonkey
Copy link
Member

One nice side effect of this change, is this line will not have an empty string.

update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] );

@kt-12 kt-12 marked this pull request as ready for review September 28, 2023 13:09
@kt-12 kt-12 changed the title Duplicate query in WP_Query as $post_type is empty for the first instance. Oct 2, 2023
Comment on lines +1425 to +1426
* @covers WP_Query::generate_cache_key
* @dataProvider data_duplicate_query_when_sticky_post
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @covers WP_Query::generate_cache_key
* @dataProvider data_duplicate_query_when_sticky_post
* @covers WP_Query::generate_cache_key
*
* @dataProvider data_duplicate_query_when_sticky_post
Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At minimum, I think we need tests that show this failing in trunk before I would feel confident making this change. However, I left a few other observations/questions. I'd love to get a 2nd set of 👀 from @peterwilsoncc or someone else who has spent more time in this part of WP_Query.

if ( empty( $post_type ) ) {
$post_type_prime = 'any';
if ( $this->is_attachment ) {
$post_type = 'attachment';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that altering the value of the $post_type variable here is safe? Seems to be used elsewhere in this function. For example, it looks like it could cause side effects for the capabilities checks @peterwilsoncc added in 51276.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, it wasn't a issue. If anything it would be a posative, as post type being empty doesn't really help ina lot of code paths.

@@ -3533,7 +3535,7 @@ public function get_posts() {

if ( $q['cache_results'] ) {
if ( $is_unfiltered_query && $unfiltered_posts === $this->posts ) {
update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
update_post_caches( $this->posts, $post_type_prime, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause any previous cases that would have passed a null value for $post_type (which would evaluate as post) to now be evaluated as any. This is probably fine, but want to ensure this is an intentional change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joemcgill Before, any empty value would have been converted to 'any' in update_post_caches.

See

if ( ! $post_type ) {
$post_type = 'any';
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most empty values that's true. But null would have been converted to the default param value of 'post'. Again, probably fine, just wanted to make that observation clear.

* @covers WP_Query::generate_cache_key
* @dataProvider data_duplicate_query_when_sticky_post
*/
public function test_duplicate_query_when_sticky_post( $is_cached_prior, $expected_num_queries ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried running these tests in trunk without the rest of the changes applied and they're already passing, which makes me think that this isn't an effective test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case should pass before and after. That is the point, to show the behaviour didn't change. This test just covers a code path that was not covered before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Jonny. In that case, I think we need another test case that demonstrates the bug and confirms that this change fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants