Skip to content

Commit

Permalink
Merge branch 'feature/orderby-post-date' into point-release/1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tlovett1 committed Apr 9, 2015
2 parents 060b9f5 + f018675 commit b2a1317
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: php

php:
- "5.5"
- "5.2"
- "5.3"

env:
- WP_VERSION=latest WP_MULTISITE=0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ After running an index, ElasticPress integrates with `WP_Query` if and only if t

* ```orderby``` (*string*)

Order results by field name instead of relevance. Currently only supports: ```title```, ```name```, and ```relevance``` (default).
Order results by field name instead of relevance. Currently only supports: ```title```, ```name```, ```date```, and ```relevance``` (default).

* ```order``` (*string*)

Expand Down
11 changes: 11 additions & 0 deletions classes/class-ep-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ protected function parse_order( $order ) {
* @access protected
*
* @param string $orderby Alias for the field to order by.
* @param string $order
* @return array|bool Array formatted value to used in the sort DSL. False otherwise.
*/
protected function parse_orderby( $orderby, $order ) {
Expand All @@ -1165,6 +1166,7 @@ protected function parse_orderby( $orderby, $order ) {
'relevance',
'name',
'title',
'date',
);

if ( ! in_array( $orderby, $allowed_keys ) ) {
Expand All @@ -1182,6 +1184,15 @@ protected function parse_orderby( $orderby, $order ) {
),
);
break;
case 'date':
$sort = array(
array(
'post_date' => array(
'order' => $order,
),
),
);
break;
case 'name':
case 'title':
$sort = array(
Expand Down
31 changes: 31 additions & 0 deletions tests/test-single-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,37 @@ public function testSearchPostTitleOrderbyQuery() {
$this->assertEquals( 'ordertest 111', $query->posts[2]->post_title );
}

/**
* Test post_date orderby query
*
* @since 1.4
*/
public function testSearchPostDateOrderbyQuery() {
ep_create_and_sync_post( array( 'post_title' => 'ordertes 333' ) );
sleep( 3 );

ep_create_and_sync_post( array( 'post_title' => 'ordertest 111' ) );
sleep( 3 );

ep_create_and_sync_post( array( 'post_title' => 'ordertest 222' ) );

ep_refresh_index();

$args = array(
's' => 'ordertest',
'orderby' => 'date',
'order' => 'DESC',
);

$query = new WP_Query( $args );

$this->assertEquals( 3, $query->post_count );
$this->assertEquals( 3, $query->found_posts );
$this->assertEquals( 'ordertes 333', $query->posts[0]->post_title );
$this->assertEquals( 'ordertest 111', $query->posts[1]->post_title );
$this->assertEquals( 'ordertest 222', $query->posts[2]->post_title );
}

/**
* Test relevance orderby query advanced
*
Expand Down

0 comments on commit b2a1317

Please sign in to comment.