edit_post( array|null $post_data = null ): int
- Since
- 1.5.0
- Source
wp-admin/includes/post.php:259
$_POST.Description
If post data is passed as an argument, it is treated as an array of data keyed appropriately for turning into a post object.
If post data is not passed, the $_POST global variable is used instead.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$post_dataarray|nulloptional- The array of post data to process.
Defaults to the$_POSTsuperglobal.Default:null
Return value
int- Post ID.
Performance profile
How much work a call to edit_post() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 99–169
- Plugin surface
- 1 hook
- Called by
- 6
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 467.
Third-party callbacks on 'attachment_fields_to_save' run inside this call, and their cost is not bounded by anything here.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below edit_post() - cacheobject cache
wp_cache_delete()one call below edit_post()
Further down the call graph this can also reach serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 32 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost edit_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!post_type_supports() && !isset($post_data) && !is_wp_error() | 99–112 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 106–119 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 110–118 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 111–124 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 111–124 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 112–127 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 117–125 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 119–134 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 122–130 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 122–130 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 123–133 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 124–139 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
20 further outcomes, up to 169 instructions
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 124–139 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !is_wp_error() && $image_alt === false | 125–138 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
!post_type_supports() && !isset($post_data) && !is_wp_error() | 130–140 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
!post_type_supports() && !is_wp_error() && $image_alt === false | 132–145 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 135–145 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !isset($post_data) && !is_wp_error() | 135–145 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !is_wp_error() && $image_alt === false | 136–144 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt === false | 137–150 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt === false | 137–150 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !is_wp_error() && $image_alt !== false | 138–151 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
!post_type_supports() && !is_wp_error() && $image_alt === false | 143–151 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
!post_type_supports() && !is_wp_error() && $image_alt !== false | 145–158 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt === false | 148–156 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt === false | 148–156 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !is_wp_error() && $image_alt !== false | 149–157 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt !== false | 150–163 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt !== false | 150–163 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
!post_type_supports() && !is_wp_error() && $image_alt !== false | 156–164 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt !== false | 161–169 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), unstick_post() |
current_user_can() && !post_type_supports() && !is_wp_error() && $image_alt !== false | 161–169 | get_post(), get_post_type_object(), current_user_can(), post_type_supports(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), apply_filters(), add_meta(), get_current_user_id(), update_post_meta(), wp_update_post(), wp_update_post(), _fix_attachment_links(), wp_set_post_lock(), current_user_can(), current_user_can(), stick_post() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 467 | 99–169 | 63 | |
| 8.5 | 467 | 99–169 | 63 | |
| 8.4 | 467 | 99–169 | 63 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 470 | 99–169 | 63 | |
| 8.2 | 470 | 99–169 | 63 | 1 more instruction than PHP 8.1 |
| 8.1 | 469 | 99–169 | 63 | Different branch profile from PHP 7.4 |
| 7.4 | 469 | 99–170 | 63 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while edit_post() runs, in this order:
- apply_filters( attachment_fields_to_save )filterline 435 (+176 into the body)
Filters the attachment fields to be saved.
Uses · 38
- get_post()Retrieves post data given a post ID or post object.
- sanitize_key()Sanitizes a string key.
- get_post_type_object()Retrieves a post type object by name.
- current_user_can()Returns whether the current user has the specified capability.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- __()Retrieves the translation of $text.
- post_type_supports()Checks a post type's support for a given feature.
- wp_get_post_revisions()Returns all revisions of specified post.
- _wp_get_post_revision_version()Gets the post revision version.
- _wp_upgrade_revisions_of_post()Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1.
- _wp_translate_postdata()Renames `$_POST` data from form names to DB post columns.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Show all 38
- _wp_get_allowed_postdata()Returns only allowed post data fields.
- set_post_format()Assign a format to a post
- update_post_meta()Updates a post meta field based on the given post ID.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- sanitize_url()Sanitizes a URL for database or redirect usage.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- wp_filter_post_kses()Sanitizes content for allowed HTML tags for post content.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_get_attachment_id3_keys()Returns useful keys to use to lookup data from an attachment's stored metadata.
- sanitize_text_field()Sanitizes a string from user input or from the database.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- get_post_meta_by_id()Returns post meta data by meta ID.
- is_protected_meta()Determines whether a meta key is considered protected.
- update_meta()Updates post meta data by meta ID.
- delete_meta()Deletes post meta data by meta ID.
- get_post_meta()Retrieves a post meta field for the given post ID.
- wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- add_meta()Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
- get_current_user_id()Gets the current user's ID.
- wp_update_post()Updates a post with new post data.
- _fix_attachment_links()Replaces hrefs of attachment anchors with up-to-date permalinks.
- wp_set_post_lock()Marks the post as currently being edited by the current user.
- stick_post()Makes a post sticky.
- unstick_post()Un-sticks a post.
Used by · 6
- post_preview()Saves a draft or manually autosaves for the purpose of showing a post preview.
- wp_ajax_add_meta()Handles adding meta via AJAX.
- wp_ajax_inline_save()Handles Quick Edit saving a post from a list table via AJAX.
- wp_ajax_wp_fullscreen_save_post()Handles saving posts from the fullscreen editor via AJAX.
- wp_autosave()Saves a post submitted with XHR.
- wp_write_post()Creates a new post from the "Write Post" form using `$_POST` information.
Source code
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 ] ) ) {Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-admin/includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.