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

Add compatibility with 'orderby' => 'none' #3318

Merged
merged 2 commits into from
Feb 14, 2023
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
9 changes: 9 additions & 0 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,15 @@ protected function parse_orderby( $orderbys, $default_order, $args ) {
continue;
}

/**
* If `orderby` is 'none', WordPress will let the database decide on what should be used to order.
* It will use the primary key ASC.
*/
if ( 'none' === $orderby_clause ) {
$orderby_clause = 'ID';
$order = 'asc';
}

if ( in_array( $orderby_clause, [ 'meta_value', 'meta_value_num' ], true ) ) {
if ( empty( $args['meta_key'] ) ) {
continue;
Expand Down
30 changes: 30 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,36 @@ public function testRandOrderby() {
$this->assertEquals( 3, $query->found_posts );
}

/**
* Test orderby 'none'
*
* In this case, EP should order by ID ASC, as this is the behavior used by the database.
*
* @since 4.5.0
* @group post
*/
public function testNoneOrderbyQuery() {
$posts = [];
$posts[] = $this->ep_factory->post->create( array( 'post_title' => 'ordertest 1' ) );
$posts[] = $this->ep_factory->post->create( array( 'post_title' => 'ordertest 2' ) );
$posts[] = $this->ep_factory->post->create( array( 'post_title' => 'ordertest 3' ) );

ElasticPress\Elasticsearch::factory()->refresh_indices();

$args = array(
'ep_integrate' => true,
'fields' => 'ids',
'orderby' => 'none',
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );

$this->assertEquals( 3, $query->post_count );
$this->assertEquals( $posts, $query->posts );
}

/**
* Test that a post being directly deleted gets correctly removed from the Elasticsearch index
*
Expand Down