admin管理员组文章数量:1124757
I am still in the process of learning how to use the WordPress REST API and am attempting to retrieve all posts which have been assigned the custom status "archived".
I have already created the custom status using the code below:
add_action( 'init', 'post_archived_status' ); function post_archived_status() { register_post_status( 'archived', array( 'label' => _x( 'Archived', 'post' ), 'label_count' => _n_noop( 'Archived (%s)', 'Archived (%s)'), 'public' => false, 'exclude_from_search' => true, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'publicly_queryable' => true, )); }
With the status now created, I tried to use this endpoint to retrieve the filtered posts but received an error message instead.
{
"code": "rest_invalid_param",
"message": "Invalid parameter(s): status",
"data": {
"status": 400,
"params": {
"status": "Status is forbidden."
},
"details": {
"status": {
"code": "rest_forbidden_status",
"message": "Status is forbidden.",
"data": {
"status": 401
}
}
}
}
}
I have done some research and believe it may be something to do with authorisation however, I wouldn't have thought that would be needed as I have set publicly_queryable
to true
?
Any help with this is much appreciated.
I am still in the process of learning how to use the WordPress REST API and am attempting to retrieve all posts which have been assigned the custom status "archived".
I have already created the custom status using the code below:
add_action( 'init', 'post_archived_status' ); function post_archived_status() { register_post_status( 'archived', array( 'label' => _x( 'Archived', 'post' ), 'label_count' => _n_noop( 'Archived (%s)', 'Archived (%s)'), 'public' => false, 'exclude_from_search' => true, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'publicly_queryable' => true, )); }
With the status now created, I tried to use this endpoint to retrieve the filtered posts but received an error message instead.
https://www.website.com/wp-json/wp/v2/posts?status=archived
{
"code": "rest_invalid_param",
"message": "Invalid parameter(s): status",
"data": {
"status": 400,
"params": {
"status": "Status is forbidden."
},
"details": {
"status": {
"code": "rest_forbidden_status",
"message": "Status is forbidden.",
"data": {
"status": 401
}
}
}
}
}
I have done some research and believe it may be something to do with authorisation however, I wouldn't have thought that would be needed as I have set publicly_queryable
to true
?
Any help with this is much appreciated.
Share Improve this question asked Feb 21, 2024 at 10:08 SamuelSamuel 111 bronze badge 1- Did my answer help, or did you find another solution? – Sally CJ Commented Feb 22, 2024 at 12:06
1 Answer
Reset to default 0Yes, authentication is needed, because the Posts endpoint will check whether the current user has the edit_posts
capability – see WP_REST_Posts_Controller::sanitize_post_statuses()
. Also, a HTTP status of 401
normally indicates that authentication is required.
So try again, but with an authenticated request, i.e. make sure the current user is logged-in and has the edit_posts
capability.
本文标签: REST APIRetrieving posts with custom status
版权声明:本文标题:REST API - Retrieving posts with custom status 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736643660a1946055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论