wp_insert_post_data WordPress Filter Hook

The wp_insert_post_data hook is a Wordpress hook that allows you to modify the data that is going to be inserted into a post before it is actually inserted. This can be useful if you want to make sure that certain data is always included in a post, or if you want to modify the data that is being inserted based on some other criteria.

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

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


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().

$update

(bool)Whether this is an existing post being updated.


Top ↑

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

$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'


Top ↑

Source

File: wp-includes/post.php

View on Trac



Top ↑

Changelog

Changelog
VersionDescription
6.0.0The $update parameter was added.
5.4.1The $unsanitized_postarr parameter was added.
2.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.

Show More
Show More