{
"code": "rest_invalid_param",
"message": "Invalid parameter(s): per_page",
"data": {
"status": 400,
"params": {
"per_page": "per_page must be between 1 (inclusive) and 100 (inclusive)"
}
}
}
我试图使用wp JSON API从woo-commerce中获取150个类别。我不能使它100.有什么办法来解决这个问题,使用钩子。
add_action( 'rest_product_query', 'product_override_per_page' );
/*
* params is the query array passed to WP_Query
*/
function product_override_per_page( $params ) {
if ( isset( $params ) AND isset( $params[ 'posts_per_page' ] ) ) {
$params[ 'posts_per_page' ] = "200";
}
return $params;
}
不要错过在钩子中用你的职位类型替换客户。
看着这里的文档。https:/developer.wordpress.orgrest-apiusing-the rest-apipagination。
看起来per_page参数可以在1到100之间有任何整数值。所以你的问题的答案是 没有
如果你想硬编码
/**
* Retrieves the query params for the collections.
*
* @since 4.7.0
*
* @return array Query parameters for the collection.
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param(),
'page' => array(
'description' => __( 'Current page of the collection.' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
),
'per_page' => array(
'description' => __( 'Maximum number of items to be returned in result set.' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 1000,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'search' => array(
'description' => __( 'Limit results to those matching a string.' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
);
}
wp-includesrest-apiendpointsclass-wp-rest-controller.php #L381