1

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.

1
  • Did my answer help, or did you find another solution?
    – Sally CJ
    Feb 22 at 12:06

1 Answer 1

0

Yes, authentication is needed, because the Posts endpoint will check whether the current user has the edit_posts capabilitysee 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.