Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2579 from websupporter/relevance-search
Browse files Browse the repository at this point in the history
Adding relevance orderby
  • Loading branch information
joehoyle committed Aug 18, 2016
2 parents 9cb8702 + 417b290 commit 4b05bdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/endpoints/class-wp-rest-posts-controller.php
Expand Up @@ -85,6 +85,12 @@ public function get_items_permissions_check( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {

//Make sure a search string is set in case the orderby is set to 'relevace'
if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term in order to use the relevance search.' ), array( 'status' => 400 ) );
}

$args = array();
$args['author__in'] = $request['author'];
$args['author__not_in'] = $request['author_exclude'];
Expand Down Expand Up @@ -1708,6 +1714,7 @@ public function get_collection_params() {
'default' => 'date',
'enum' => array(
'date',
'relevance',
'id',
'include',
'title',
Expand Down
9 changes: 9 additions & 0 deletions tests/test-rest-posts-controller.php
Expand Up @@ -270,6 +270,15 @@ public function test_get_items_order_and_orderby() {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );

//Relevance orderby
$this->factory->post->create( array( 'post_title' => 'Coffee Cake is relevant', 'post_content' => 'Apple', 'post_status' => 'publish' ) );
$this->factory->post->create( array( 'post_title' => 'Coffee Cake', 'post_content' => 'Apple is less relevant', 'post_status' => 'publish' ) );
$request->set_param( 'search', 'relevant' );
$request->set_param( 'orderby', 'relevance' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'Coffee Cake is relevant', $data[0]['title']['rendered'] );
}

public function test_get_items_ignore_sticky_posts_by_default() {
Expand Down

0 comments on commit 4b05bdc

Please sign in to comment.