WP_REST_Posts_Controller::handle_status_param() WordPress Method

The WP_REST_Posts_Controller::handle_status_param() function allows you to set the post status for a given post. This can be useful for managing your posts, as well as for setting up a workflow for your site.

WP_REST_Posts_Controller::handle_status_param( string $post_status, WP_Post_Type $post_type ) #

Determines validity and normalizes the given status parameter.


Parameters

$post_status

(string)(Required)Post status.

$post_type

(WP_Post_Type)(Required)Post type.


Top ↑

Return

(string|WP_Error) Post status or WP_Error if lacking the proper permission.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

	protected function handle_status_param( $post_status, $post_type ) {

		switch ( $post_status ) {
			case 'draft':
			case 'pending':
				break;
			case 'private':
				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
					return new WP_Error(
						'rest_cannot_publish',
						__( 'Sorry, you are not allowed to create private posts in this post type.' ),
						array( 'status' => rest_authorization_required_code() )
					);
				}
				break;
			case 'publish':
			case 'future':
				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
					return new WP_Error(
						'rest_cannot_publish',
						__( 'Sorry, you are not allowed to publish posts in this post type.' ),
						array( 'status' => rest_authorization_required_code() )
					);
				}
				break;
			default:
				if ( ! get_post_status_object( $post_status ) ) {
					$post_status = 'draft';
				}
				break;
		}

		return $post_status;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.