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

Feature/1520 find related improvements #1653

Merged
merged 5 commits into from
Mar 25, 2022
Merged
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
23 changes: 19 additions & 4 deletions includes/classes/Feature/RelatedPosts/RelatedPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions tests/php/features/TestRelatedPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down