Make WordPress Core


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

    r40376 r41760  
    249249        $this->assertEquals( 'Apple', $data[1]['name'] );
    250250        $this->assertEquals( 'Cantaloupe', $data[2]['name'] );
     251    }
     252
     253    public function test_get_items_orderby_slugs() {
     254        $this->factory->tag->create( array( 'name' => 'Burrito' ) );
     255        $this->factory->tag->create( array( 'name' => 'Taco' ) );
     256        $this->factory->tag->create( array( 'name' => 'Chalupa' ) );
     257
     258        $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     259        $request->set_param( 'orderby', 'include_slugs' );
     260        $request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
     261        $response = $this->server->dispatch( $request );
     262        $data = $response->get_data();
     263        $this->assertEquals( 200, $response->get_status() );
     264        $this->assertEquals( 'taco', $data[0]['slug'] );
     265        $this->assertEquals( 'burrito', $data[1]['slug'] );
     266        $this->assertEquals( 'chalupa', $data[2]['slug'] );
    251267    }
    252268
Note: See TracChangeset for help on using the changeset viewer.