Skip to content
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
55 changes: 43 additions & 12 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3628,20 +3628,51 @@ public function get_posts() {

// Fetch sticky posts that weren't in the query results.
if ( ! empty( $sticky_posts ) ) {
$stickies = get_posts(
array(
'post__in' => $sticky_posts,
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => count( $sticky_posts ),
'suppress_filters' => $query_vars['suppress_filters'],
'cache_results' => $query_vars['cache_results'],
'update_post_meta_cache' => $query_vars['update_post_meta_cache'],
'update_post_term_cache' => $query_vars['update_post_term_cache'],
'lazy_load_term_meta' => $query_vars['lazy_load_term_meta'],
)
$sticky_query_args = array(
'post__in' => $sticky_posts,
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => count( $sticky_posts ),
'suppress_filters' => $query_vars['suppress_filters'],
'cache_results' => $query_vars['cache_results'],
'update_post_meta_cache' => $query_vars['update_post_meta_cache'],
'update_post_term_cache' => $query_vars['update_post_term_cache'],
'lazy_load_term_meta' => $query_vars['lazy_load_term_meta'],
);

/*
* Honor constraining query vars from the current query when fetching
* sticky posts, so that stickies unrelated to the requested author,
* slug, term, or other explicit filters are not prepended to the
* results.
*/
$constraining_query_vars = array(
'author__in',
'author__not_in',
'post_name__in',
'category__in',
'category__not_in',
'category__and',
'tag__in',
'tag__not_in',
'tag__and',
'tag_slug__in',
'tag_slug__and',
'post_parent',
'post_parent__in',
'post_parent__not_in',
'tax_query',
'meta_query',
);

foreach ( $constraining_query_vars as $constraining_query_var ) {
if ( ! empty( $query_vars[ $constraining_query_var ] ) ) {
$sticky_query_args[ $constraining_query_var ] = $query_vars[ $constraining_query_var ];
}
}

$stickies = get_posts( $sticky_query_args );

foreach ( $stickies as $sticky_post ) {
array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
++$sticky_offset;
Expand Down
110 changes: 110 additions & 0 deletions tests/phpunit/tests/query/stickies.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,114 @@ static function () use ( $post_ids ) {

$this->assertSame( $sticky_query_vars['posts_per_page'], $sticky_count );
}

/**
* Stickies fetched for injection should honor the query's author__in constraint:
* stickies by other authors must not be prepended, while stickies matching the
* constraint are still moved to the top.
*
* @ticket 63360
*/
public function test_stickies_should_obey_author__in() {
$author_1 = self::factory()->user->create( array( 'role' => 'author' ) );
$author_2 = self::factory()->user->create( array( 'role' => 'author' ) );

$now = time();

$sticky_by_author_1 = self::factory()->post->create(
array(
'post_author' => $author_1,
'post_date' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
)
);
$plain_by_author_1 = self::factory()->post->create(
array(
'post_author' => $author_1,
'post_date' => gmdate( 'Y-m-d H:i:s', $now - 10 ),
)
);
$sticky_by_author_2 = self::factory()->post->create(
array(
'post_author' => $author_2,
'post_date' => gmdate( 'Y-m-d H:i:s', $now ),
)
);

stick_post( $sticky_by_author_1 );
stick_post( $sticky_by_author_2 );

$q = new WP_Query(
array(
'author__in' => array( $author_1 ),
)
);

unstick_post( $sticky_by_author_1 );
unstick_post( $sticky_by_author_2 );

$this->assertSame(
array( $sticky_by_author_1, $plain_by_author_1 ),
wp_list_pluck( $q->posts, 'ID' )
);
}

/**
* Stickies fetched for injection should honor the query's post_name__in
* constraint: querying specific slugs must not prepend unrelated stickies.
*
* @ticket 63360
*/
public function test_stickies_should_obey_post_name__in() {
$post_id = self::factory()->post->create(
array(
'post_name' => 'not-sticky-1',
)
);

$sticky_id = self::factory()->post->create(
array(
'post_name' => 'sticky-unrelated',
)
);

stick_post( $sticky_id );

$q = new WP_Query(
array(
'post_name__in' => array( 'not-sticky-1' ),
)
);

unstick_post( $sticky_id );

$this->assertSame( array( $post_id ), wp_list_pluck( $q->posts, 'ID' ) );
}

/**
* Stickies fetched for injection should honor the query's category__not_in
* constraint: stickies assigned to an excluded term must not be prepended.
*
* @ticket 63360
*/
public function test_stickies_should_obey_category__not_in() {
$category_id = self::factory()->category->create();

$sticky_in_excluded_category = self::factory()->post->create(
array(
'post_category' => array( $category_id ),
)
);

stick_post( $sticky_in_excluded_category );

$q = new WP_Query(
array(
'category__not_in' => array( $category_id ),
)
);

unstick_post( $sticky_in_excluded_category );

$this->assertNotContains( $sticky_in_excluded_category, wp_list_pluck( $q->posts, 'ID' ) );
}
}
Loading