admin管理员组文章数量:1279235
I registered a custom post type called wasb_message
with show_in_rest
set to true
and rest_base
set to 'messages'
so the resource is reachable at endpoint https://mydomain/wp-json/wp/v2/messages (using the WP_REST_Posts_Controller
class).
The custom post type has a custom meta field called wasb_status
with an integer value, how can I add a custom parameter so when I do a get request to endpoint https://mydomain/wp-json/wp/v2/messages?status=2 I retrieve messages with wasb_status
equal to 2 without register a new route?
I read this code reference page and this old question but I can't get it working, using Postman to send get request I get:
{
"code": "rest_invalid_param",
"message": "Parametro(i) non valido(i): status",
"data": {
"status": 400,
"params": {
"status": "Stato non consentito."
},
"details": {
"status": {
"code": "rest_forbidden_status",
"message": "Stato non consentito.",
"data": {
"status": 401
}
}
}
}
}
I registered a custom post type called wasb_message
with show_in_rest
set to true
and rest_base
set to 'messages'
so the resource is reachable at endpoint https://mydomain/wp-json/wp/v2/messages (using the WP_REST_Posts_Controller
class).
The custom post type has a custom meta field called wasb_status
with an integer value, how can I add a custom parameter so when I do a get request to endpoint https://mydomain/wp-json/wp/v2/messages?status=2 I retrieve messages with wasb_status
equal to 2 without register a new route?
I read this code reference page and this old question but I can't get it working, using Postman to send get request I get:
{
"code": "rest_invalid_param",
"message": "Parametro(i) non valido(i): status",
"data": {
"status": 400,
"params": {
"status": "Stato non consentito."
},
"details": {
"status": {
"code": "rest_forbidden_status",
"message": "Stato non consentito.",
"data": {
"status": 401
}
}
}
}
}
Share
Improve this question
asked Oct 12, 2021 at 10:51
icolumbroicolumbro
791 silver badge9 bronze badges
2 Answers
Reset to default 1I solved following this answer and writing:
public function query_wasb_messages_by_status( $args, $request ) {
if ( ! is_null( $request->get_param( 'message_status' ) ) ) {
$args['meta_query'] = array(
'_wasb_message_status' => array(
'key' => '_wasb_message_status',
'value' => (int) $request->get_param( 'message_status' ),
'compare' => '=',
'type' => 'numeric'
)
);
}
return $args;
}
add_filter( 'rest_wasb_message_query', array( $this, 'query_wasb_messages_by_status'), 10, 2 );
My problem is I used status
as parameter name but status
already exists for posts ('draft', 'published', etc...) so I changed the name to message_status
...
As per the reply by Haris it should be something like this:
function wasb_rest_parameter($args, $request) {
if( isset($request["wasb_status"]) && is_numeric($request["wasb_status"]) ) {
$args['meta_key'] = 'wasb_status';
$args['meta_value'] = intval($request["wasb_status"]);
}
return $args;
}
add_filter('rest_wasb_message_query', 'wasb_rest_parameter', 10, 2);
Not yet tested. If it does not work, will update the answer.
本文标签: Add custom parameter to REST API request of a custom post type
版权声明:本文标题:Add custom parameter to REST API request of a custom post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274974a2369675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论