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
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, 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