REST API: Fix handling of some orderby parameters for the Posts controller.

- 'orderby' => 'include' requires an array of post_ids via the include collection param.
- 'orderby' => 'id' and 'orderby' => 'slug' need map the correct WP_Query equivalents. 

Merges [39440] to the 4.7 branch.

Props flixos90, hnle, dd32, rachelbaker, joehoyle, pento.
Fixes #38971 for 4.7.

Built from https://develop.svn.wordpress.org/branches/4.7@39441


git-svn-id: http://core.svn.wordpress.org/branches/4.7@39381 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Rachel Baker 2016-12-02 22:21:33 +00:00
parent 4726c85ee5
commit 3adc537233
2 changed files with 17 additions and 3 deletions

View File

@ -148,6 +148,11 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
}
// Ensure an include parameter is set in case the orderby is set to 'include'.
if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
return new WP_Error( 'rest_orderby_include_missing_include', sprintf( __( 'Missing parameter(s): %s' ), 'include' ), array( 'status' => 400 ) );
}
// Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params();
$args = array();
@ -836,8 +841,17 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$query_args['ignore_sticky_posts'] = true;
}
if ( 'include' === $query_args['orderby'] ) {
$query_args['orderby'] = 'post__in';
// Map to proper WP_Query orderby param.
if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
$orderby_mappings = array(
'id' => 'ID',
'include' => 'post__in',
'slug' => 'post_name',
);
if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
}
}
return $query_args;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-RC1-39439';
$wp_version = '4.7-RC1-39441';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.