function sanitize_post_field($field, $value, $post_id, $context) {}

Sanitize post field based on context.
Possible context values are: ‘raw’, ‘edit’, ‘db’, ‘display’, ‘attribute’ and ‘js’. The ‘display’ context is used by default. ‘attribute’ and ‘js’ contexts are treated like ‘display’ when calling filters.


  • string $field: The Post Object field name.
  • mixed $value: The Post Object value.
  • int $post_id: Post ID.
  • string $context: How to sanitize post fields. Looks for ‘raw’, ‘edit’, ‘db’, ‘display’, ‘attribute’ and ‘js’.

Return values

returns:Sanitized value.

Defined filters

  • edit_{$field}
    apply_filters("edit_{$field}", $value, $post_id)
  • {$field_no_prefix}_edit_pre
    apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id)

Source code

function sanitize_post_field($field, $value, $post_id, $context) {

	$int_fields = array('ID', 'post_parent', 'menu_order');

	if ( in_array($field, $int_fields) )

		$value = (int) $value;

	// Fields which contain arrays of ints.

	$array_int_fields = array( 'ancestors' );

	if ( in_array($field, $array_int_fields) ) {

		$value = array_map( 'absint', $value);

		return $value;


	if ( 'raw' == $context )

		return $value;

	$prefixed = false;

	if ( false !== strpos($field, 'post_') ) {

		$prefixed = true;

		$field_no_prefix = str_replace('post_', '', $field);


	if ( 'edit' == $context ) {

		$format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');

		if ( $prefixed ) {

			$value = apply_filters("edit_{$field}", $value, $post_id);

			// Old school

			$value = apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);

		} else {


No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: