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

Author caching in WP_Query::the_post() can trigger an error. #3553

Conversation

peterwilsoncc
Copy link
Contributor

@peterwilsoncc peterwilsoncc commented Nov 2, 2022

https://core.trac.wordpress.org/ticket/56948

The first commit ensures the tests are failing prior to the changes, the second to only warm the cache when WP_Query::$posts is populated with WP_Post objects.

Two things are tested:

1) That requesting post objects warms the author cache
2) That requesting a subset of fields will not throw an error
@peterwilsoncc peterwilsoncc force-pushed the fix/56948-warm-authors-with-post-objects branch from f09540d to c9502de Compare November 3, 2022 00:22
@peterwilsoncc peterwilsoncc marked this pull request as ready for review November 3, 2022 00:29
Comment on lines 3574 to 3575
$post_ids = array_filter( $this->posts, 'is_numeric' );
$post_ids = array_filter( $post_ids );
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
$post_ids = array_filter( $this->posts, 'is_numeric' );
$post_ids = array_filter( $post_ids );
$post_ids = array_filter( array_filter( $this->posts, 'is_numeric' ) );

We could do this, might be a little cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd pushed 3b5fca1 before seeing this, on the basis that comments are needed to explain why array_filter is called twice.

_prime_post_caches( $post_ids, $this->query_vars['update_post_term_cache'], $this->query_vars['update_post_meta_cache'] );
}
$post_objects = array_map( 'get_post', $this->posts );
update_post_author_caches( $post_objects );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It occurs to me, this will have a similar problem to #56952 and throw an error if called before pluggable functions are loaded.

It might be worth returning early in update_post_author_caches() if cache_users() is undefined.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed.

@peterwilsoncc
Copy link
Contributor Author

@peterwilsoncc peterwilsoncc deleted the fix/56948-warm-authors-with-post-objects branch November 9, 2022 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants