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

Prevent using the EP if query has unsuported orderby #3273

Merged
merged 10 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Prevent using the EP if query has unsuported orderby
  • Loading branch information
burhandodhy committed Jan 25, 2023
commit e3dc48dd05adb3eec6226646500f87c1e00ba9ac
23 changes: 23 additions & 0 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function search_setup() {
add_filter( 'ep_post_filters', [ $this, 'exclude_posts_from_search' ], 10, 3 );
add_action( 'post_submitbox_misc_actions', [ $this, 'output_exclude_from_search_setting' ] );
add_action( 'edit_post', [ $this, 'save_exclude_from_search_meta' ], 10, 2 );
add_filter( 'ep_skip_query_integration', [ $this, 'skip_query_integration' ], 10, 2 );
}


Expand Down Expand Up @@ -786,4 +787,26 @@ public function save_exclude_from_search_meta( $post_id, $post ) {

update_post_meta( $post_id, 'ep_exclude_from_search', $exclude_from_search );
}

/**
* If WP_Query has unsupported orderby, skip ES query integration and use the WP query instead.
*
* @param bool $skip Whether to skip ES query integration
* @param \WP_Query $query WP_Query object
* @since 4.5
*/
function skip_query_integration( $skip, $query ) {
$unsupported_orderby = [
'post__in',
'post_name__in',
'post_parent__in',
'parent'
];

if ( in_array( $query->get( 'orderby' ), $unsupported_orderby, true ) ) {
return true;
}

return $skip;
}
}
35 changes: 35 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8337,4 +8337,39 @@ public function testDeletingThumbnailShouldNotUpdateRelatedNonIndexablePost() {
$ep_post = ElasticPress\Indexables::factory()->get( 'post' )->get( $product_id );
$this->assertArrayHasKey( '_thumbnail_id', $ep_post['meta'] );
}

/**
* Test that query with unsupported orderby does not use EP.
*
* @since 4.5.0
*/
public function testQueryWithUnSupportedOrderByDoesNotUseEP() {
// test for post__in
$query = new \WP_Query( array(
'orderby' => 'post__in',
'ep_integrate' => true,
) );
$this->assertNull( $query->elasticsearch_success );

// test for post_name__in
$query = new \WP_Query( array(
'orderby' => 'post_name__in',
'ep_integrate' => true,
) );
$this->assertNull( $query->elasticsearch_success );

// test for post_parent__in
$query = new \WP_Query( array(
'orderby' => 'post_parent__in',
'ep_integrate' => true,
) );
$this->assertNull( $query->elasticsearch_success );

// test for parent
$query = new \WP_Query( array(
'orderby' => 'parent',
'ep_integrate' => true,
) );
$this->assertNull( $query->elasticsearch_success );
}
}