WordPress.org

Make WordPress Core

Changeset 39093


Ignore:
Timestamp:
11/02/2016 07:13:24 AM (4 years ago)
Author:
rmccue
Message:

REST API: Allow querying for multiple slug values.

Props jnylen0, rachelbaker.
Fixes #38579.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39089 r39093  
    170170            'parent_exclude' => 'post_parent__not_in',
    171171            'search'         => 's',
    172             'slug'           => 'name',
     172            'slug'           => 'post_name__in',
    173173            'status'         => 'post_status',
    174174        );
     
    883883            'posts_per_page',
    884884            'date_query',
     885            'post_name__in',
    885886        );
    886887
     
    21132114
    21142115        $params['slug'] = array(
    2115             'description'       => __( 'Limit result set to posts with a specific slug.' ),
    2116             'type'              => 'string',
    2117             'validate_callback' => 'rest_validate_request_arg',
     2116            'description'       => __( 'Limit result set to posts with one or more specific slugs.' ),
     2117            'type'              => 'array',
     2118            'sanitize_callback' => 'wp_parse_slug_list',
    21182119        );
    21192120
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39084 r39093  
    253253        $this->assertEquals( 1, count( $data ) );
    254254        $this->assertEquals( 'Apple', $data[0]['title']['rendered'] );
     255    }
     256
     257    public function test_get_items_multiple_slugs_array_query() {
     258        $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
     259        $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
     260        $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) );
     261        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     262        $request->set_param( 'slug', array( 'banana', 'peach' ) );
     263        $response = $this->server->dispatch( $request );
     264        $this->assertEquals( 200, $response->get_status() );
     265        $data = $response->get_data();
     266        $this->assertEquals( 2, count( $data ) );
     267        $titles = array(
     268            $data[0]['title']['rendered'],
     269            $data[1]['title']['rendered'],
     270        );
     271        sort( $titles );
     272        $this->assertEquals( array( 'Banana', 'Peach' ), $titles );
     273    }
     274
     275    public function test_get_items_multiple_slugs_string_query() {
     276        $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
     277        $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
     278        $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) );
     279        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     280        $request->set_param( 'slug', 'apple,banana' );
     281        $response = $this->server->dispatch( $request );
     282        $this->assertEquals( 200, $response->get_status() );
     283        $data = $response->get_data();
     284        $this->assertEquals( 2, count( $data ) );
     285        $titles = array(
     286            $data[0]['title']['rendered'],
     287            $data[1]['title']['rendered'],
     288        );
     289        sort( $titles );
     290        $this->assertEquals( array( 'Apple', 'Banana' ), $titles );
    255291    }
    256292
Note: See TracChangeset for help on using the changeset viewer.