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

Sticky Posts logic is pretty broken #65

Open
WPprodigy opened this issue Oct 13, 2020 · 0 comments
Open

Sticky Posts logic is pretty broken #65

WPprodigy opened this issue Oct 13, 2020 · 0 comments

Comments

@WPprodigy
Copy link

WPprodigy commented Oct 13, 2020

Here, EP attempts to add a weight to sticky posts if on doing the homepage query: https://github.com/Automattic/ElasticPress/blob/develop/includes/classes/Indexable/Post/Post.php#L1188-L1217

This is how the core WP Query does it: https://github.com/WordPress/WordPress/blob/6b7ba33d6851fea58bc3214c2b9e4316aa918fa5/wp-includes/class-wp-query.php#L3131-L3133

I believe the checks in EP are not sufficient / errorsome, and should be more along these lines:

$sticky_posts  = get_option( 'sticky_posts' );
$on_first_page = ! isset( $args['paged'] ) || $args['paged'] <= 1;
$ignore_sticky = isset( $q['ignore_sticky_posts'] ) && $q['ignore_sticky_posts'];

// Check first if there's sticky posts and show them only in the front page.
if ( is_array( $sticky_posts ) && ! empty( $sticky_posts ) && is_home() && $on_first_page && ! $ignore_sticky ) {

Furthermore, these tests are broken / not actually testing anything: https://github.com/Automattic/ElasticPress/blob/develop/tests/php/indexables/TestPost.php#L4102-L4142. The second testStickyPostsExcludedOnNotHome test also falls into the same issue as #63 - it will sometimes fails if based on if the posts share a timestamp or not.

The tests are really meant to be testing the prioritization of sticky posts, but first need to of course send that test home query to ES. Something like this:

public function testStickyPostsPrioritizedOnHome() {
	// Setup three posts, with the sticky post chronologically in the middle.
	Functions\create_and_sync_post( array( 'post_title' => 'Post 1', 'post_date' => '2020-08-25 12:30:00' ) );
	$sticky_id = Functions\create_and_sync_post( array( 'post_title' => 'Post 2: Sticky', 'post_date' => '2020-08-25 12:31:00' ) );
	stick_post( $sticky_id );
	Functions\create_and_sync_post( array( 'post_title' => 'Post 3', 'post_date' => '2020-08-25 12:32:00' ) );
	ElasticPress\Elasticsearch::factory()->refresh_indices();

	add_filter( 'pre_get_posts', [ $this, 'send_home_query_to_es' ] );

	$this->go_to( '/' );
	$q = $GLOBALS['wp_query'];
	$this->assertEquals( 'Post 2: Sticky', $q->posts[0]->post_title );

	remove_filter( 'pre_get_posts', [ $this, 'send_home_query_to_es' ] );
}

Just making this an issue for now, as it's turned into a bit more of a timesink than intended. If anybody else wants to work on this, be sure to make sure the tests can actually fail if the special sticky posts logic is removed, rather than just relying on WP_Query core logic to carry the burden.

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

No branches or pull requests

1 participant