# edit_post()

URL: https://chugunov.pro/api-wordpress/functions/edit_post/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 1.5.0.

## Сигнатура

```php
edit_post( array|null $post_data = null ): int
```

## Описание

Если данные записи переданы как аргумент, они рассматриваются как массив данных с соответствующими ключами для преобразования в объект записи.
Если данные записи не переданы, вместо них используется глобальная переменная $_POST.

## Параметры

- `$post_data` `array|null` — необязательный, по умолчанию `null`. Массив данных записи для обработки.
  
  По умолчанию — суперглобальная переменная $_POST.

## Возвращаемое значение

`int`

## Исходный код

Файл: `wp-admin/includes/post.php:255`

```php
function edit_post( $post_data = null ) {
	global $wpdb;

	if ( empty( $post_data ) ) {
		$post_data = &$_POST;
	}

	// Clear out any data in internal vars.
	unset( $post_data['filter'] );

	$post_id = (int) $post_data['post_ID'];
	$post    = get_post( $post_id );

	$post_data['post_type']      = $post->post_type;
	$post_data['post_mime_type'] = $post->post_mime_type;

	if ( ! empty( $post_data['post_status'] ) ) {
		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );

		if ( 'inherit' === $post_data['post_status'] ) {
			unset( $post_data['post_status'] );
		}
	}

	$ptype = get_post_type_object( $post_data['post_type'] );
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		if ( 'page' === $post_data['post_type'] ) {
			wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
		} else {
			wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
		}
	}

	if ( post_type_supports( $ptype->name, 'revisions' ) ) {
		$revisions = wp_get_post_revisions(
			$post_id,
			array(
				'order'          => 'ASC',
				'posts_per_page' => 1,
			)
		);
		$revision  = current( $revisions );

		// Check if the revisions have been upgraded.
		if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
			_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_id ) );
		}
	}

	if ( isset( $post_data['visibility'] ) ) {
		switch ( $post_data['visibility'] ) {
			case 'public':
				$post_data['post_password'] = '';
				break;
			case 'password':
				unset( $post_data['sticky'] );
				break;
			case 'private':
				$post_data['post_status']   = 'private';
				$post_data['post_password'] = '';
				unset( $post_data['sticky'] );
				break;
		}
	}

	$post_data = _wp_translate_postdata( true, $post_data );
	if ( is_wp_error( $post_data ) ) {
		wp_die( $post_data->get_error_message() );
	}
	$translated = _wp_get_allowed_postdata( $post_data );

	// Post formats.
	if ( isset( $post_data['post_format'] ) ) {
		set_post_format( $post_id, $post_data['post_format'] );
	}

	$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
	foreach ( $format_meta_urls as $format_meta_url ) {
		$keyed = '_format_' . $format_meta_url;
		if ( isset( $post_data[ $keyed ] ) ) {
			update_post_meta( $post_id, $keyed, wp_slash( sanitize_url( wp_unslash( $post_data[ $keyed ] ) ) ) );
		}
	}

	$format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );

	foreach ( $format_keys as $key ) {
		$keyed = '_format_' . $key;
		if ( isset( $post_data[ $keyed ] ) ) {
			if ( current_user_can( 'unfiltered_html' ) ) {
				update_post_meta( $post_id, $keyed, $post_data[ $keyed ] );
			} else {
				update_post_meta( $post_id, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
			}
		}
	}

	if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
		$id3data = wp_get_attachment_metadata( $post_id );
		if ( ! is_array( $id3data ) ) {
			$id3data = array();
		}

		foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
			if ( isset( $post_data[ 'id3_' . $key ] ) ) {
				$id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
			}
		}
		wp_update_attachment_metadata( $post_id, $id3data );
	}

	// Meta stuff.
	if ( isset( $post_data['meta'] ) && $post_data['meta'] ) {
		foreach ( $post_data['meta'] as $key => $value ) {
			$meta = get_post_meta_by_id( $key );
			if ( ! $meta ) {
				continue;
			}

			if ( (int) $meta->post_id !== $post_id ) {
				continue;
			}

			if ( is_protected_meta( $meta->meta_key, 'post' )
				|| ! current_user_can( 'edit_post_meta', $post_id, $meta->meta_key )
			) {
				continue;
			}

			if ( is_protected_meta( $value['key'], 'post' )
				|| ! current_user_can( 'edit_post_meta', $post_id, $value['key'] )
			) {
				continue;
			}

			update_meta( $key, $value['key'], $value['value'] );
		}
	}

	if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) {
		foreach ( $post_data['deletemeta'] as $key => $value ) {
			$meta = get_post_meta_by_id( $key );
			if ( ! $meta ) {
				continue;
			}

			if ( (int) $meta->post_id !== $post_id ) {
				continue;
			}

			if ( is_protected_meta( $meta->meta_key, 'post' )
				|| ! current_user_can( 'delete_post_meta', $post_id, $meta->meta_key )
			) {
				continue;
			}

			delete_meta( $key );
		}
	}

	// Attachment stuff.
	if ( 'attachment' === $post_data['post_type'] ) {
		if ( isset( $post_data['_wp_attachment_image_alt'] ) ) {
			$image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );

			if ( get_post_meta( $post_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
				$image_alt = wp_strip_all_tags( $image_alt, true );

				// update_post_meta() expects slashed.
				update_post_meta( $post_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
			}
		}

		$attachment_data = $post_data['attachments'][ $post_id ] ?? array();

		/** This filter is documented in wp-admin/includes/media.php */
		$translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
	}

	// Convert taxonomy input to term IDs, to avoid ambiguity.
	if ( isset( $post_data['tax_input'] ) ) {
		foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
			$tax_object = get_taxonomy( $taxonomy );

			if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
				$translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
			}
		}
	}

	add_meta( $post_id );

	update_post_meta( $post_id, '_edit_last', get_current_user_id() );

	$success = wp_update_post( $translated );

	// If the save failed, see if we can confidence check the main fields and try again.
	if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
		$fields = array( 'post_title', 'post_content', 'post_excerpt' );

		foreach ( $fields as $field ) {
			if ( isset( $translated[ $field ] ) ) {
				$translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] );
			}
		}

		wp_update_post( $translated );
	}

	// Now that we have an ID we can fix any attachment anchor hrefs.
	_fix_attachment_links( $post_id );

	wp_set_post_lock( $post_id );

	if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
		if ( ! empty( $post_data['sticky'] ) ) {
			stick_post( $post_id );
		} else {
			unstick_post( $post_id );
		}
	}

	return $post_id;
}
```

## История изменений

- 1.5.0 — Introduced.

## Связанные

Использует: [`_wp_get_allowed_postdata`](https://chugunov.pro/api-wordpress/functions/_wp_get_allowed_postdata/), `wpdb::strip_invalid_text_for_column`, [`wp_set_post_lock`](https://chugunov.pro/api-wordpress/functions/wp_set_post_lock/), [`get_post_meta_by_id`](https://chugunov.pro/api-wordpress/functions/get_post_meta_by_id/), [`update_meta`](https://chugunov.pro/api-wordpress/functions/update_meta/), [`delete_meta`](https://chugunov.pro/api-wordpress/functions/delete_meta/), [`add_meta`](https://chugunov.pro/api-wordpress/functions/add_meta/), [`_fix_attachment_links`](https://chugunov.pro/api-wordpress/functions/_fix_attachment_links/), [`_wp_translate_postdata`](https://chugunov.pro/api-wordpress/functions/_wp_translate_postdata/), [`wp_strip_all_tags`](https://chugunov.pro/api-wordpress/functions/wp_strip_all_tags/), [`wp_filter_post_kses`](https://chugunov.pro/api-wordpress/functions/wp_filter_post_kses/), [`wp_get_attachment_id3_keys`](https://chugunov.pro/api-wordpress/functions/wp_get_attachment_id3_keys/), [`wp_get_attachment_metadata`](https://chugunov.pro/api-wordpress/functions/wp_get_attachment_metadata/), [`wp_update_attachment_metadata`](https://chugunov.pro/api-wordpress/functions/wp_update_attachment_metadata/), [`wp_update_post`](https://chugunov.pro/api-wordpress/functions/wp_update_post/), [`stick_post`](https://chugunov.pro/api-wordpress/functions/stick_post/), [`unstick_post`](https://chugunov.pro/api-wordpress/functions/unstick_post/), [`post_type_supports`](https://chugunov.pro/api-wordpress/functions/post_type_supports/), [`update_post_meta`](https://chugunov.pro/api-wordpress/functions/update_post_meta/), [`wp_get_post_revisions`](https://chugunov.pro/api-wordpress/functions/wp_get_post_revisions/), [`_wp_get_post_revision_version`](https://chugunov.pro/api-wordpress/functions/_wp_get_post_revision_version/), [`_wp_upgrade_revisions_of_post`](https://chugunov.pro/api-wordpress/functions/_wp_upgrade_revisions_of_post/), [`set_post_format`](https://chugunov.pro/api-wordpress/functions/set_post_format/), [`is_protected_meta`](https://chugunov.pro/api-wordpress/functions/is_protected_meta/), [`current_user_can`](https://chugunov.pro/api-wordpress/functions/current_user_can/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`wp_slash`](https://chugunov.pro/api-wordpress/functions/wp_slash/), [`wp_unslash`](https://chugunov.pro/api-wordpress/functions/wp_unslash/), [`sanitize_text_field`](https://chugunov.pro/api-wordpress/functions/sanitize_text_field/), [`sanitize_key`](https://chugunov.pro/api-wordpress/functions/sanitize_key/), [`sanitize_url`](https://chugunov.pro/api-wordpress/functions/sanitize_url/), [`wp_die`](https://chugunov.pro/api-wordpress/functions/wp_die/), [`get_taxonomy`](https://chugunov.pro/api-wordpress/functions/get_taxonomy/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_current_user_id`](https://chugunov.pro/api-wordpress/functions/get_current_user_id/), [`get_post_meta`](https://chugunov.pro/api-wordpress/functions/get_post_meta/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/), [`get_post_type_object`](https://chugunov.pro/api-wordpress/functions/get_post_type_object/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/).
Используется в: [`post_preview`](https://chugunov.pro/api-wordpress/functions/post_preview/), [`wp_autosave`](https://chugunov.pro/api-wordpress/functions/wp_autosave/), [`wp_write_post`](https://chugunov.pro/api-wordpress/functions/wp_write_post/), [`wp_ajax_wp_fullscreen_save_post`](https://chugunov.pro/api-wordpress/functions/wp_ajax_wp_fullscreen_save_post/), [`wp_ajax_add_meta`](https://chugunov.pro/api-wordpress/functions/wp_ajax_add_meta/), [`wp_ajax_inline_save`](https://chugunov.pro/api-wordpress/functions/wp_ajax_inline_save/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/edit_post/
