Title: wp_insert_post_data
Published: April 25, 2014
Last modified: August 20, 2026

---

# apply_filters( ‘wp_insert_post_data’, array $data, array $postarr, array $unsanitized_postarr, bool $update )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#user-contributed-notes)

Filters slashed post data just before it is inserted into the database.

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

 `$data`array

An array of slashed, sanitized, and processed post data.

`$postarr`array

An array of sanitized (and slashed) but otherwise unmodified post data.

`$unsanitized_postarr`array

An array of slashed yet *unsanitized* and unprocessed post data as originally passed
to [wp_insert_post()](https://developer.wordpress.org/reference/functions/wp_insert_post/).

`$update`bool

Whether this is an existing post being updated.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#more-information)󠁿

You must pass the value 2 for the `$accepted_args` argument in `add_filter()` if
you want to access `$postarr`.

Some have problems to get the post ID inside wp_insert_post_data:

If you have access to $postarr, you can easily retrieve the post ID with

    ```php
    $my_post_id = $postarr['ID'];
    ```

The defaults for the parameter $data are:
 ` 'post_author', 'post_date', 'post_date_gmt','
post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status','
post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping','
pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid'

The defaults for the parameter $postarr are:
 ` 'post_status' - Default is 'draft'.'
post_type' - Default is 'post'. 'post_author' - Default is current user ID ($user_ID).
The ID of the user who added the post. 'ping_status' - Default is the value in '
default_ping_status' option. Whether the attachment can accept pings. 'post_parent'-
Default is 0. Set this for the post it belongs to, if any. 'menu_order' - Default
is 0. The order it is displayed. 'to_ping' - Whether to ping. 'pinged' - Default
is empty string. 'post_password' - Default is empty string. The password to access
the attachment. 'guid' - Global Unique ID for referencing the attachment. 'post_content_filtered'-
Post content filtered. 'post_excerpt' - Post excerpt.

The post $postarr looks like:
 ` 'post_status', 'post_type', 'post_author', 'ping_status','
post_parent', 'menu_order', 'to_ping', 'pinged', 'post_password', 'guid', 'post_content_filtered','
post_excerpt', 'import_id', 'post_content', 'post_title', 'ID', 'post_date', 'post_date_gmt','
comment_status', 'post_name', 'post_modified', 'post_modified_gmt', 'post_mime_type','
comment_count', 'ancestors', 'post_category', 'tags_input', 'filter'

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

    ```php
    $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post.php#L4978)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post.php#L4978-L4978)

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

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

Inserts or updates a post in the database.

  |

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

| Version | Description | 
| [6.0.0](https://developer.wordpress.org/reference/since/6.0.0/) | The `$update` parameter was added. | 
| [5.4.1](https://developer.wordpress.org/reference/since/5.4.1/) | The `$unsanitized_postarr` parameter was added. | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#comment-content-5345)
 2.    [Dnyanesh Mahajan](https://profiles.wordpress.org/dnyaneshmj/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/#comment-5345)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-5345)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-5345)
 4.      ```php
         //Removing &amp; or any html special enitiy from name
         function filter_post_name( $data, $postarr, $unsanitized_postarr){
             $data['post_title'] = html_entity_decode($data['post_title']);
             return $data;
         }
     
         add_filter( 'wp_insert_post_data', 'filter_post_name',10,3);
         ```
     
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%3Freplytocom%3D5345%23feedback-editor-5345)
 6.   [Skip to note 5 content](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#comment-content-6127)
 7.    [Feast Design Co.](https://profiles.wordpress.org/feastdesignco/)  [  4 years ago  ](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/#comment-6127)
 8.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-6127)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-6127)
 9.  Contrary to the name, this hook also runs on post update and post delete.
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%3Freplytocom%3D6127%23feedback-editor-6127)
 11.  [Skip to note 6 content](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/?output_format=md#comment-content-7313)
 12.   [usman0304](https://profiles.wordpress.org/usman0304/)  [  2 years ago  ](https://developer.wordpress.org/reference/hooks/wp_insert_post_data/#comment-7313)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-7313)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%23comment-7313)
 14. remove spaces from post titles
 15.     ```php
         add_filter( 'wp_insert_post_data', function( $data ) {
             // for a specific post type
             if ( 'post' === $data['post_type'] ) {
                 $data['post_title'] = preg_replace( '/\s+/', '', $data['post_title'] );
             }
     
             return $data;
         } );
         ```
     
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_insert_post_data%2F%3Freplytocom%3D7313%23feedback-editor-7313)

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