From bbbf3a97fad5b0aa6be338cb57936d8dfc33d530 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Wed, 2 Nov 2016 07:13:24 +0000 Subject: [PATCH] REST API: Allow querying for multiple `slug` values. Props jnylen0, rachelbaker. Fixes #38579. git-svn-id: https://develop.svn.wordpress.org/trunk@39093 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 9 ++--- .../tests/rest-api/rest-posts-controller.php | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 4beda26210..0122f9a773 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -169,7 +169,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { 'parent' => 'post_parent__in', 'parent_exclude' => 'post_parent__not_in', 'search' => 's', - 'slug' => 'name', + 'slug' => 'post_name__in', 'status' => 'post_status', ); @@ -882,6 +882,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { 'post_parent__not_in', 'posts_per_page', 'date_query', + 'post_name__in', ); $valid_vars = array_merge( $valid_vars, $rest_valid ); @@ -2112,9 +2113,9 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { } $params['slug'] = array( - 'description' => __( 'Limit result set to posts with a specific slug.' ), - 'type' => 'string', - 'validate_callback' => 'rest_validate_request_arg', + 'description' => __( 'Limit result set to posts with one or more specific slugs.' ), + 'type' => 'array', + 'sanitize_callback' => 'wp_parse_slug_list', ); $params['status'] = array( diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index d941f22d6a..dcebfe8422 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -254,6 +254,42 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( 'Apple', $data[0]['title']['rendered'] ); } + public function test_get_items_multiple_slugs_array_query() { + $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) ); + $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) ); + $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'slug', array( 'banana', 'peach' ) ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $this->assertEquals( 2, count( $data ) ); + $titles = array( + $data[0]['title']['rendered'], + $data[1]['title']['rendered'], + ); + sort( $titles ); + $this->assertEquals( array( 'Banana', 'Peach' ), $titles ); + } + + public function test_get_items_multiple_slugs_string_query() { + $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) ); + $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) ); + $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'slug', 'apple,banana' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $this->assertEquals( 2, count( $data ) ); + $titles = array( + $data[0]['title']['rendered'], + $data[1]['title']['rendered'], + ); + sort( $titles ); + $this->assertEquals( array( 'Apple', 'Banana' ), $titles ); + } + public function test_get_items_status_query() { wp_set_current_user( 0 ); $this->factory->post->create( array( 'post_status' => 'draft' ) );