Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 6b31826

Browse files
committed
Move /posts WP_Query vars to filter param
Currently, `/wp/v2/posts` allows WP_Query params and a few of our own "per_page" etc. This is confusing and inconsistant. I think we discussed a while back that we'd put all the WP_Query (internal) named collection params under a `filter` param (inspired by v1) and implement standardized collection params for posts that's inline with the params for `terms`, `users` and `comments`.
1 parent bfaee00 commit 6b31826

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ public function register_routes() {
2727
'default' => 10,
2828
'sanitize_callback' => 'absint',
2929
),
30+
'filter' => array(),
3031
);
3132

32-
foreach ( $this->get_allowed_query_vars() as $var ) {
33-
if ( ! isset( $posts_args[ $var ] ) ) {
34-
$posts_args[ $var ] = array();
35-
}
36-
}
37-
3833
register_rest_route( 'wp/v2', '/' . $base, array(
3934
array(
4035
'methods' => WP_REST_Server::READABLE,
@@ -89,12 +84,17 @@ public function register_routes() {
8984
* @return WP_Error|WP_REST_Response
9085
*/
9186
public function get_items( $request ) {
92-
$args = (array) $request->get_params();
93-
$args['post_type'] = $this->post_type;
94-
$args['paged'] = $args['page'];
95-
$args['posts_per_page'] = $args['per_page'];
87+
$args = array();
88+
$args['post_type'] = $this->post_type;
89+
$args['paged'] = $request['page'];
90+
$args['posts_per_page'] = $request['per_page'];
9691
unset( $args['page'] );
9792

93+
if ( isset( $request['filter'] ) ) {
94+
$args = array_merge( $args, $request['filter'] );
95+
unset( $args['filter'] );
96+
}
97+
9898
/**
9999
* Alter the query arguments for a request.
100100
*

tests/test-rest-posts-controller.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ public function test_get_items() {
5050
public function test_get_items_empty_query() {
5151
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
5252
$request->set_query_params( array(
53-
'type' => 'post',
54-
'year' => 2008,
53+
'filter' => array( 'year' => 2008 ),
5554
) );
5655
$response = $this->server->dispatch( $request );
57-
5856
$this->assertEquals( array(), $response->get_data() );
5957
$this->assertEquals( 200, $response->get_status() );
6058
}

0 commit comments

Comments
 (0)