Make WordPress Core


Ignore:
Timestamp:
10/05/2017 12:36:43 AM (8 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-categories-controller.php

    r41737 r41760  
    288288    }
    289289
     290    public function test_get_items_orderby_slugs() {
     291        $this->factory->category->create( array( 'name' => 'Burrito' ) );
     292        $this->factory->category->create( array( 'name' => 'Taco' ) );
     293        $this->factory->category->create( array( 'name' => 'Chalupa' ) );
     294
     295        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     296        $request->set_param( 'orderby', 'include_slugs' );
     297        $request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
     298        $response = $this->server->dispatch( $request );
     299        $data = $response->get_data();
     300        $this->assertEquals( 200, $response->get_status() );
     301        $this->assertEquals( 'taco', $data[0]['slug'] );
     302        $this->assertEquals( 'burrito', $data[1]['slug'] );
     303        $this->assertEquals( 'chalupa', $data[2]['slug'] );
     304    }
     305
    290306    protected function post_with_categories() {
    291307        $post_id = $this->factory->post->create();
Note: See TracChangeset for help on using the changeset viewer.