Title: WP_REST_Posts_Controller::create_item
Published: December 6, 2016
Last modified: August 20, 2026

---

# WP_REST_Posts_Controller::create_item( WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#wp--skip-link--target)

Creates a single post.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Full details about the request.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Response
object on success, or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#source)󠁿

    ```php
    public function create_item( $request ) {
    	if ( ! empty( $request['id'] ) ) {
    		return new WP_Error(
    			'rest_post_exists',
    			__( 'Cannot create existing post.' ),
    			array( 'status' => 400 )
    		);
    	}

    	$prepared_post = $this->prepare_item_for_database( $request );

    	if ( is_wp_error( $prepared_post ) ) {
    		return $prepared_post;
    	}

    	$prepared_post->post_type = $this->post_type;

    	if ( ! empty( $prepared_post->post_name )
    		&& ! empty( $prepared_post->post_status )
    		&& in_array( $prepared_post->post_status, array( 'draft', 'pending' ), true )
    	) {
    		/*
    		 * `wp_unique_post_slug()` returns the same slug for 'draft' or 'pending' posts.
    		 *
    		 * To ensure that a unique slug is generated, pass the post data with the 'publish' status.
    		 */
    		$prepared_post->post_name = wp_unique_post_slug(
    			$prepared_post->post_name,
    			$prepared_post->id,
    			'publish',
    			$prepared_post->post_type,
    			$prepared_post->post_parent
    		);
    	}

    	$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true, false );

    	if ( is_wp_error( $post_id ) ) {

    		if ( 'db_insert_error' === $post_id->get_error_code() ) {
    			$post_id->add_data( array( 'status' => 500 ) );
    		} else {
    			$post_id->add_data( array( 'status' => 400 ) );
    		}

    		return $post_id;
    	}

    	$post = get_post( $post_id );

    	/**
    	 * Fires after a single post is created or updated via the REST API.
    	 *
    	 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
    	 *
    	 * Possible hook names include:
    	 *
    	 *  - `rest_insert_post`
    	 *  - `rest_insert_page`
    	 *  - `rest_insert_attachment`
    	 *
    	 * @since 4.7.0
    	 *
    	 * @param WP_Post         $post     Inserted or updated post object.
    	 * @param WP_REST_Request $request  Request object.
    	 * @param bool            $creating True when creating a post, false when updating.
    	 */
    	do_action( "rest_insert_{$this->post_type}", $post, $request, true );

    	$schema = $this->get_item_schema();

    	if ( ! empty( $schema['properties']['sticky'] ) ) {
    		if ( ! empty( $request['sticky'] ) ) {
    			stick_post( $post_id );
    		} else {
    			unstick_post( $post_id );
    		}
    	}

    	if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
    		$this->handle_featured_media( $request['featured_media'], $post_id );
    	}

    	if ( ! empty( $schema['properties']['format'] ) && ! empty( $request['format'] ) ) {
    		set_post_format( $post, $request['format'] );
    	}

    	if ( ! empty( $schema['properties']['template'] ) && isset( $request['template'] ) ) {
    		$this->handle_template( $request['template'], $post_id, true );
    	}

    	$terms_update = $this->handle_terms( $post_id, $request );

    	if ( is_wp_error( $terms_update ) ) {
    		return $terms_update;
    	}

    	if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    		$meta_update = $this->meta->update_value( $request['meta'], $post_id );

    		if ( is_wp_error( $meta_update ) ) {
    			return $meta_update;
    		}
    	}

    	$post          = get_post( $post_id );
    	$fields_update = $this->update_additional_fields_for_object( $post, $request );

    	if ( is_wp_error( $fields_update ) ) {
    		return $fields_update;
    	}

    	$request->set_param( 'context', 'edit' );

    	/**
    	 * Fires after a single post is completely created or updated via the REST API.
    	 *
    	 * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
    	 *
    	 * Possible hook names include:
    	 *
    	 *  - `rest_after_insert_post`
    	 *  - `rest_after_insert_page`
    	 *  - `rest_after_insert_attachment`
    	 *
    	 * @since 5.0.0
    	 *
    	 * @param WP_Post         $post     Inserted or updated post object.
    	 * @param WP_REST_Request $request  Request object.
    	 * @param bool            $creating True when creating a post, false when updating.
    	 */
    	do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );

    	wp_after_insert_post( $post, false, null );

    	$response = $this->prepare_item_for_response( $post, $request );
    	$response = rest_ensure_response( $response );

    	$response->set_status( 201 );
    	$response->header( 'Location', rest_url( rest_get_route_for_post( $post ) ) );

    	return $response;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L741)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L741-L883)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#hooks)󠁿

 [do_action( “rest_after_insert_{$this->post_type}”, WP_Post $post, WP_REST_Request $request, bool $creating )](https://developer.wordpress.org/reference/hooks/rest_after_insert_this-post_type/)

Fires after a single post is completely created or updated via the REST API.

 [do_action( “rest_insert_{$this->post_type}”, WP_Post $post, WP_REST_Request $request, bool $creating )](https://developer.wordpress.org/reference/hooks/rest_insert_this-post_type/)

Fires after a single post is created or updated via the REST API.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_after_insert_post()](https://developer.wordpress.org/reference/functions/wp_after_insert_post/)`wp-includes/post.php` |

Fires actions after a post, its terms and meta data has been saved.

  | 
| [rest_get_route_for_post()](https://developer.wordpress.org/reference/functions/rest_get_route_for_post/)`wp-includes/rest-api.php` |

Gets the REST API route for a post.

  | 
| [WP_REST_Posts_Controller::get_item_schema()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/get_item_schema/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Retrieves the post’s schema, conforming to JSON Schema.

  | 
| [WP_REST_Posts_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Prepares a single post output for response.

  | 
| [WP_REST_Posts_Controller::handle_featured_media()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_featured_media/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Determines the featured media based on a request param.

  | 
| [WP_REST_Posts_Controller::handle_template()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_template/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Sets the template for a post.

  | 
| [WP_REST_Posts_Controller::handle_terms()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_terms/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Updates the post’s terms from a REST request.

  | 
| [WP_REST_Posts_Controller::prepare_item_for_database()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_database/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Prepares a single post for create or update.

  | 
| [wp_unique_post_slug()](https://developer.wordpress.org/reference/functions/wp_unique_post_slug/)`wp-includes/post.php` |

Computes a unique slug for the post, when given the desired slug and some post details.

  | 
| [wp_insert_post()](https://developer.wordpress.org/reference/functions/wp_insert_post/)`wp-includes/post.php` |

Inserts or updates a post in the database.

  | 
| [stick_post()](https://developer.wordpress.org/reference/functions/stick_post/)`wp-includes/post.php` |

Makes a post sticky.

  | 
| [unstick_post()](https://developer.wordpress.org/reference/functions/unstick_post/)`wp-includes/post.php` |

Un-sticks a post.

  | 
| [set_post_format()](https://developer.wordpress.org/reference/functions/set_post_format/)`wp-includes/post-formats.php` |

Assign a format to a post

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [rest_url()](https://developer.wordpress.org/reference/functions/rest_url/)`wp-includes/rest-api.php` |

Retrieves the URL to a REST endpoint.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_slash()](https://developer.wordpress.org/reference/functions/wp_slash/)`wp-includes/formatting.php` |

Adds slashes to a string or recursively adds slashes to strings within an array.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 16 more](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#)

| Used by | Description | 
| [WP_REST_Font_Faces_Controller::create_item()](https://developer.wordpress.org/reference/classes/wp_rest_font_faces_controller/create_item/)`wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php` |

Creates a font face for the parent font family.

  | 
| [WP_REST_Font_Families_Controller::create_item()](https://developer.wordpress.org/reference/classes/wp_rest_font_families_controller/create_item/)`wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php` |

Creates a single font family.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/create_item/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_posts_controller%2Fcreate_item%2F)
before being able to contribute a note or feedback.