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

Commit fe3056f

Browse files
Merge pull request #1975 from WP-API/924-declare-parent-pages
Declare `parent` query param for Pages
2 parents 7573f8a + ecf3992 commit fe3056f

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,17 @@ public function get_collection_params() {
16031603
'slug',
16041604
),
16051605
);
1606+
1607+
$post_type_obj = get_post_type_object( $this->post_type );
1608+
if ( $post_type_obj->hierarchical ) {
1609+
$params['parent'] = array(
1610+
'description' => _( 'Limit result set to that of a specific parent id.' ),
1611+
'type' => 'integer',
1612+
'sanitize_callback' => 'absint',
1613+
'default' => null,
1614+
);
1615+
}
1616+
16061617
$params['status'] = array(
16071618
'default' => 'attachment' === $this->post_type ? 'inherit' : 'publish',
16081619
'description' => __( 'Limit result set to posts assigned a specific status.' ),

tests/test-rest-pages-controller.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,47 @@ public function test_context_param() {
4343
$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
4444
}
4545

46+
public function test_registered_query_params() {
47+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' );
48+
$response = $this->server->dispatch( $request );
49+
$data = $response->get_data();
50+
$keys = array_keys( $data['endpoints'][0]['args'] );
51+
sort( $keys );
52+
$this->assertEquals( array(
53+
'author',
54+
'context',
55+
'filter',
56+
'include',
57+
'order',
58+
'orderby',
59+
'page',
60+
'parent',
61+
'per_page',
62+
'search',
63+
'status',
64+
), $keys );
65+
}
66+
4667
public function test_get_items() {
4768

4869
}
4970

71+
public function test_get_items_parent_query() {
72+
$id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page' ) );
73+
$id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_type' => 'page', 'post_parent' => $id1 ) );
74+
// No parent
75+
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
76+
$response = $this->server->dispatch( $request );
77+
$data = $response->get_data();
78+
$this->assertEquals( 2, count( $data ) );
79+
// Filter to parent
80+
$request->set_param( 'parent', $id1 );
81+
$response = $this->server->dispatch( $request );
82+
$data = $response->get_data();
83+
$this->assertEquals( 1, count( $data ) );
84+
$this->assertEquals( $id2, $data[0]['id'] );
85+
}
86+
5087
public function test_get_items_private_filter_query_var() {
5188
// Private query vars inaccessible to unauthorized users
5289
wp_set_current_user( 0 );

tests/test-rest-posts-controller.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ public function test_context_param() {
5050
$this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
5151
}
5252

53+
public function test_registered_query_params() {
54+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
55+
$response = $this->server->dispatch( $request );
56+
$data = $response->get_data();
57+
$keys = array_keys( $data['endpoints'][0]['args'] );
58+
sort( $keys );
59+
$this->assertEquals( array(
60+
'author',
61+
'context',
62+
'filter',
63+
'include',
64+
'order',
65+
'orderby',
66+
'page',
67+
'per_page',
68+
'search',
69+
'status',
70+
), $keys );
71+
}
72+
5373
public function test_get_items() {
5474
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
5575
$response = $this->server->dispatch( $request );

0 commit comments

Comments
 (0)