Query: Improved sticky post query

Ensure that the parameters `update_post_meta_cache`, `update_post_term_cache`, `cache_results`, `suppress_filters` and 
`lazy_load_term_meta` that are passed to WP_Query, are also passed down to the secondary query used to get sticky 
posts. 

Props Spacedmonkey, peterwilsoncc, rehanali, uday17035. 
Fixes #36907.



git-svn-id: https://develop.svn.wordpress.org/trunk@52982 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2022-03-23 10:37:11 +00:00
parent d7f9f4405b
commit b3d6638139
2 changed files with 30 additions and 4 deletions

View File

@ -3260,10 +3260,14 @@ class WP_Query {
if ( ! empty( $sticky_posts ) ) {
$stickies = get_posts(
array(
'post__in' => $sticky_posts,
'post_type' => $post_type,
'post_status' => 'publish',
'nopaging' => true,
'cache_results' => $q['cache_results'],
'lazy_load_term_meta' => $q['lazy_load_term_meta'],
'post__in' => $sticky_posts,
'post_type' => $post_type,
'post_status' => 'publish',
'suppress_filters' => $q['suppress_filters'],
'update_post_meta_cache' => $q['update_post_meta_cache'],
'update_post_term_cache' => $q['update_post_term_cache'],
)
);

View File

@ -4,6 +4,7 @@
* Tests related to sticky functionality in WP_Query.
*
* @group query
* @covers WP_Query::get_posts
*/
class Tests_Query_Stickies extends WP_UnitTestCase {
public static $posts = array();
@ -104,4 +105,25 @@ class Tests_Query_Stickies extends WP_UnitTestCase {
public function set_post__not_in( $q ) {
$q->set( 'post__not_in', array( self::$posts[8] ) );
}
/**
* @ticket 36907
*/
public function test_stickies_nest_query() {
$filter = new MockAction();
add_filter( 'posts_pre_query', array( $filter, 'filter' ), 10, 2 );
$this->go_to( '/' );
$filter_args = $filter->get_args();
$query_vars = $filter_args[0][1]->query_vars;
$sticky_query_vars = $filter_args[1][1]->query_vars;
$this->assertNotEmpty( $sticky_query_vars['posts_per_page'] );
$this->assertSame( $query_vars['suppress_filters'], $sticky_query_vars['suppress_filters'] );
$this->assertSame( $query_vars['update_post_meta_cache'], $sticky_query_vars['update_post_meta_cache'] );
$this->assertSame( $query_vars['update_post_term_cache'], $sticky_query_vars['update_post_term_cache'] );
$this->assertSame( $query_vars['lazy_load_term_meta'], $sticky_query_vars['lazy_load_term_meta'] );
$this->assertSame( $query_vars['cache_results'], $sticky_query_vars['cache_results'] );
$this->assertTrue( $sticky_query_vars['ignore_sticky_posts'] );
$this->assertTrue( $sticky_query_vars['no_found_rows'] );
}
}