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

---

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

## In this article

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

Prepares a single post for create or update.

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

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

Request object.

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

 stdClass|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
Post object or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/).

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

    ```php
    protected function prepare_item_for_database( $request ) {
    	$prepared_post  = new stdClass();
    	$current_status = '';

    	// Post ID.
    	if ( isset( $request['id'] ) ) {
    		$existing_post = $this->get_post( $request['id'] );
    		if ( is_wp_error( $existing_post ) ) {
    			return $existing_post;
    		}

    		$prepared_post->ID = $existing_post->ID;
    		$current_status    = $existing_post->post_status;
    	}

    	$schema = $this->get_item_schema();

    	// Post title.
    	if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
    		if ( is_string( $request['title'] ) ) {
    			$prepared_post->post_title = $request['title'];
    		} elseif ( ! empty( $request['title']['raw'] ) ) {
    			$prepared_post->post_title = $request['title']['raw'];
    		}
    	}

    	// Post content.
    	if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
    		if ( is_string( $request['content'] ) ) {
    			$prepared_post->post_content = $request['content'];
    		} elseif ( isset( $request['content']['raw'] ) ) {
    			$prepared_post->post_content = $request['content']['raw'];
    		}
    	}

    	// Post excerpt.
    	if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
    		if ( is_string( $request['excerpt'] ) ) {
    			$prepared_post->post_excerpt = $request['excerpt'];
    		} elseif ( isset( $request['excerpt']['raw'] ) ) {
    			$prepared_post->post_excerpt = $request['excerpt']['raw'];
    		}
    	}

    	// Post type.
    	if ( empty( $request['id'] ) ) {
    		// Creating new post, use default type for the controller.
    		$prepared_post->post_type = $this->post_type;
    	} else {
    		// Updating a post, use previous type.
    		$prepared_post->post_type = get_post_type( $request['id'] );
    	}

    	$post_type = get_post_type_object( $prepared_post->post_type );

    	// Post status.
    	if (
    		! empty( $schema['properties']['status'] ) &&
    		isset( $request['status'] ) &&
    		( ! $current_status || $current_status !== $request['status'] )
    	) {
    		$status = $this->handle_status_param( $request['status'], $post_type );

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

    		$prepared_post->post_status = $status;
    	}

    	// Post date.
    	if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
    		$current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date : false;
    		$date_data    = rest_get_date_with_gmt( $request['date'] );

    		if ( ! empty( $date_data ) && $current_date !== $date_data[0] ) {
    			list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
    			$prepared_post->edit_date                                        = true;
    		}
    	} elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
    		$current_date = isset( $prepared_post->ID ) ? get_post( $prepared_post->ID )->post_date_gmt : false;
    		$date_data    = rest_get_date_with_gmt( $request['date_gmt'], true );

    		if ( ! empty( $date_data ) && $current_date !== $date_data[1] ) {
    			list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
    			$prepared_post->edit_date                                        = true;
    		}
    	}

    	/*
    	 * Sending a null date or date_gmt value resets date and date_gmt to their
    	 * default values (`0000-00-00 00:00:00`).
    	 */
    	if (
    		( ! empty( $schema['properties']['date_gmt'] ) && $request->has_param( 'date_gmt' ) && null === $request['date_gmt'] ) ||
    		( ! empty( $schema['properties']['date'] ) && $request->has_param( 'date' ) && null === $request['date'] )
    	) {
    		$prepared_post->post_date_gmt = null;
    		$prepared_post->post_date     = null;
    	}

    	// Post slug.
    	if ( ! empty( $schema['properties']['slug'] ) && isset( $request['slug'] ) ) {
    		$prepared_post->post_name = $request['slug'];
    	}

    	// Author.
    	if ( ! empty( $schema['properties']['author'] ) && ! empty( $request['author'] ) ) {
    		$post_author = (int) $request['author'];

    		if ( get_current_user_id() !== $post_author ) {
    			$user_obj = get_userdata( $post_author );

    			if ( ! $user_obj ) {
    				return new WP_Error(
    					'rest_invalid_author',
    					__( 'Invalid author ID.' ),
    					array( 'status' => 400 )
    				);
    			}
    		}

    		$prepared_post->post_author = $post_author;
    	}

    	// Post password.
    	if ( ! empty( $schema['properties']['password'] ) && isset( $request['password'] ) ) {
    		$prepared_post->post_password = $request['password'];

    		if ( '' !== $request['password'] ) {
    			if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
    				return new WP_Error(
    					'rest_invalid_field',
    					__( 'A post can not be sticky and have a password.' ),
    					array( 'status' => 400 )
    				);
    			}

    			if ( ! empty( $prepared_post->ID ) && is_sticky( $prepared_post->ID ) ) {
    				return new WP_Error(
    					'rest_invalid_field',
    					__( 'A sticky post can not be password protected.' ),
    					array( 'status' => 400 )
    				);
    			}
    		}
    	}

    	if ( ! empty( $schema['properties']['sticky'] ) && ! empty( $request['sticky'] ) ) {
    		if ( ! empty( $prepared_post->ID ) && post_password_required( $prepared_post->ID ) ) {
    			return new WP_Error(
    				'rest_invalid_field',
    				__( 'A password protected post can not be set to sticky.' ),
    				array( 'status' => 400 )
    			);
    		}
    	}

    	// Parent.
    	if ( ! empty( $schema['properties']['parent'] ) && isset( $request['parent'] ) ) {
    		if ( 0 === (int) $request['parent'] ) {
    			$prepared_post->post_parent = 0;
    		} else {
    			$parent = get_post( (int) $request['parent'] );

    			if ( empty( $parent ) ) {
    				return new WP_Error(
    					'rest_post_invalid_id',
    					__( 'Invalid post parent ID.' ),
    					array( 'status' => 400 )
    				);
    			}

    			$prepared_post->post_parent = (int) $parent->ID;
    		}
    	}

    	// Menu order.
    	if ( ! empty( $schema['properties']['menu_order'] ) && isset( $request['menu_order'] ) ) {
    		$prepared_post->menu_order = (int) $request['menu_order'];
    	}

    	// Comment status.
    	if ( ! empty( $schema['properties']['comment_status'] ) && ! empty( $request['comment_status'] ) ) {
    		$prepared_post->comment_status = $request['comment_status'];
    	}

    	// Ping status.
    	if ( ! empty( $schema['properties']['ping_status'] ) && ! empty( $request['ping_status'] ) ) {
    		$prepared_post->ping_status = $request['ping_status'];
    	}

    	if ( ! empty( $schema['properties']['template'] ) ) {
    		// Force template to null so that it can be handled exclusively by the REST controller.
    		$prepared_post->page_template = null;
    	}

    	/**
    	 * Applies Block Hooks to content-like post types.
    	 *
    	 * Content-like post types are those that support the editor and would benefit
    	 * from Block Hooks functionality. This replaces the individual post type filters
    	 * that were previously hardcoded in default-filters.php.
    	 *
    	 * @since 7.0.0
    	 */
    	$content_like_post_types = array( 'post', 'page', 'wp_block', 'wp_navigation' );

    	/**
    	 * Filters which post types should have Block Hooks applied.
    	 *
    	 * Allows themes and plugins to add or remove post types that should
    	 * have Block Hooks functionality enabled in the REST API.
    	 *
    	 * @since 7.0.0
    	 *
    	 * @param string[]         $content_like_post_types Array of post type names that support Block Hooks.
    	 * @param string           $post_type               The current post type being processed.
    	 * @param stdClass|WP_Post $prepared_post           The prepared post object.
    	 */
    	$content_like_post_types = apply_filters( 'rest_block_hooks_post_types', $content_like_post_types, $this->post_type, $prepared_post );

    	if ( in_array( $this->post_type, $content_like_post_types, true ) ) {
    		$prepared_post = update_ignored_hooked_blocks_postmeta( $prepared_post );
    	}

    	/**
    	 * Filters a post before it is inserted 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_pre_insert_post`
    	 *  - `rest_pre_insert_page`
    	 *  - `rest_pre_insert_attachment`
    	 *
    	 * @since 4.7.0
    	 *
    	 * @param stdClass        $prepared_post An object representing a single post prepared
    	 *                                       for inserting or updating the database.
    	 * @param WP_REST_Request $request       Request object.
    	 */
    	return apply_filters( "rest_pre_insert_{$this->post_type}", $prepared_post, $request );
    }
    ```

[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#L1288)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1288-L1532)

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

 [apply_filters( ‘rest_block_hooks_post_types’, string[] $content_like_post_types, string $post_type, stdClass|WP_Post $prepared_post )](https://developer.wordpress.org/reference/hooks/rest_block_hooks_post_types/)

Filters which post types should have Block Hooks applied.

 [apply_filters( “rest_pre_insert_{$this->post_type}”, stdClass $prepared_post, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_pre_insert_this-post_type/)

Filters a post before it is inserted via the REST API.

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

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

Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.

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

Gets the post, if the ID is valid.

  | 
| [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::handle_status_param()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_status_param/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Determines validity and normalizes the given status parameter.

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

Parses a date into both its local and UTC equivalent, in MySQL datetime format.

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

Determines whether the post requires password and whether a correct password has been provided.

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

Determines whether a post is sticky.

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

Retrieves the post type of the current post or of a given post.

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

Retrieves the translation of $text.

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

Retrieves user info by user ID.

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

Calls the callback functions that have been added to a filter hook.

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

Gets the current user’s ID.

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

Retrieves a post type object by name.

  | 
| [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 11 more](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_database/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_database/?output_format=md#)

| Used by | Description | 
| [WP_REST_Attachments_Controller::prepare_item_for_database()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_database/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment for create or update.

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

Creates a single post.

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

Updates a single post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_database/?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%2Fprepare_item_for_database%2F)
before being able to contribute a note or feedback.