diff --git a/includes/classes/Feature/RelatedPosts/RelatedPosts.php b/includes/classes/Feature/RelatedPosts/RelatedPosts.php index 8774531f70..d59f32e77a 100644 --- a/includes/classes/Feature/RelatedPosts/RelatedPosts.php +++ b/includes/classes/Feature/RelatedPosts/RelatedPosts.php @@ -114,10 +114,10 @@ public function formatted_args( $formatted_args, $args ) { * * @param int $post_id Post ID * @param int $return Return code - * @since 2.1 - * @return array|bool + * @since 4.1.0 + * @return WP_Query */ - public function find_related( $post_id, $return = 5 ) { + public function get_related_query( $post_id, $return = 5 ) { $args = array( 'more_like' => $post_id, 'posts_per_page' => $return, @@ -133,7 +133,22 @@ public function find_related( $post_id, $return = 5 ) { * @since 2.1 * @return {array} New arguments */ - $query = new WP_Query( apply_filters( 'ep_find_related_args', $args ) ); + return new WP_Query( apply_filters( 'ep_find_related_args', $args ) ); + } + + /** + * Search Elasticsearch for related content + * + * @param int $post_id Post ID + * @param int $return Return code + * + * @since 2.1 + * @uses get_related_query + * + * @return array|bool + */ + public function find_related( $post_id, $return = 5 ) { + $query = $this->get_related_query( $post_id, $return = 5 ); if ( ! $query->have_posts() ) { return false; diff --git a/tests/php/features/TestRelatedPosts.php b/tests/php/features/TestRelatedPosts.php index eac3135877..b84aa7c0de 100644 --- a/tests/php/features/TestRelatedPosts.php +++ b/tests/php/features/TestRelatedPosts.php @@ -93,6 +93,33 @@ public function testFindRelatedPostFilter() { remove_filter( 'ep_find_related_args', array( $this, 'find_related_posts_filter' ), 10, 1 ); } + /** + * Test for related posts query + * + * @group related_posts + */ + public function testGetRelatedQuery() { + $post_id = Functions\create_and_sync_post( array( 'post_content' => 'findme test 1' ) ); + + $related_post_title = 'related post test'; + Functions\create_and_sync_post( array( + 'post_title' => $related_post_title, + 'post_content' => 'findme test 2' + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + ElasticPress\Features::factory()->activate_feature( 'related_posts' ); + ElasticPress\Features::factory()->setup_features(); + + $query = ElasticPress\Features::factory()->get_registered_feature( 'related_posts' )->get_related_query( $post_id, 1 ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertNotEmpty( $query->posts ); + $this->assertEquals( '1', $query->post_count ); + $this->assertEquals( $related_post_title, $query->posts[0]->post_title ); + } + /** * Detect EP fire *