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-users-controller.php

    r41226 r41760  
    408408        $data = $response->get_data();
    409409        $this->assertEquals( $low_id, $data[0]['id'] );
     410    }
     411
     412    public function test_get_items_orderby_slugs() {
     413        wp_set_current_user( self::$user );
     414
     415        $this->factory->user->create( array( 'user_nicename' => 'burrito' ) );
     416        $this->factory->user->create( array( 'user_nicename' => 'taco' ) );
     417        $this->factory->user->create( array( 'user_nicename' => 'chalupa' ) );
     418
     419        $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     420        $request->set_param( 'orderby', 'include_slugs' );
     421        $request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
     422        $response = $this->server->dispatch( $request );
     423        $data = $response->get_data();
     424
     425        $this->assertEquals( 'taco', $data[0]['slug'] );
     426        $this->assertEquals( 'burrito', $data[1]['slug'] );
     427        $this->assertEquals( 'chalupa', $data[2]['slug'] );
    410428    }
    411429
Note: See TracChangeset for help on using the changeset viewer.