Make WordPress Core


Ignore:
Timestamp:
10/05/2017 12:36:43 AM (7 years ago)
Author:
kadamwhite
Message:

REST API: Support ordering response collection by listed slugs.

Adds an "include_slug" orderby value for REST API collections to permit returning a collection filtered by slugs in the same order in which those slugs are specified.
Previously, the order of slugs provided with the ?slug query parameter had no effect on the order of the returned records.

Props wonderboymusic, ocean90, boonebgorges.
Fixes #40826.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r40605 r41760  
    597597        $this->assertEquals( 'abc', $data[1]['slug'] );
    598598        $this->assertPostsOrderedBy( '{posts}.post_name DESC' );
     599    }
     600
     601    public function test_get_items_with_orderby_slugs() {
     602        $slugs = array( 'burrito', 'taco', 'chalupa' );
     603        foreach ( $slugs as $slug ) {
     604            $this->factory->post->create( array( 'post_title' => $slug, 'post_name' => $slug, 'post_status' => 'publish' ) );
     605        }
     606
     607        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     608        $request->set_param( 'orderby', 'include_slugs' );
     609        $request->set_param( 'slug', array( 'taco', 'chalupa', 'burrito' ) );
     610
     611        $response = $this->server->dispatch( $request );
     612        $data = $response->get_data();
     613
     614        $this->assertEquals( 'taco', $data[0]['slug'] );
     615        $this->assertEquals( 'chalupa', $data[1]['slug'] );
     616        $this->assertEquals( 'burrito', $data[2]['slug'] );
    599617    }
    600618
Note: See TracChangeset for help on using the changeset viewer.