edit_post_(post_type)
Fires after an existing post of the specified type is updated in the database.
Replace the dynamic (post_type) part with the post type name:
- edit_post_post - regular posts.
- edit_post_page - pages.
edit_post_book- the custombookpost type.
In wp_insert_post(), the action fires when updating a post (when an ID is specified) or through wp_update_post(). The call is skipped when inserting a new post.
Saving from the editor may update an already created auto-draft.
The hook is not limited to the admin area. In core, it is also called:
- In wp_publish_post(), including when publishing on schedule.
- In wp_update_comment_count_now() after the comment count is recalculated, even if the number has not changed.
- In WP_Customize_Manager::trash_changeset_post() when moving a Customizer changeset to the trash.
When updating through wp_insert_post(), the actions run in this order:
edit_post_(post_type)- edit_post
- post_updated
- save_post_(post_type)
- save_post
Infinite recursion
Calling wp_update_post() for the same post inside the callback triggers this hook again and can lead to infinite recursion. Therefore, temporarily disable your callback with remove_action() before the nested update, then add it again.
The $fire_after_hooks = false parameter of wp_insert_post() does not disable this action.
Use save_post_(post_type) to handle both post creation and updates.
Use post_updated to compare post fields before and after an update.
Use wp_after_insert_post to run after the post, its terms, and its meta-fields have been saved.
This hook is skipped when an attachment is updated through wp_insert_post(): use edit_attachment or attachment_updated.
Usage
add_action( 'edit_post_(post_type)', 'wp_kama_edit_post_type_action', 10, 2 );
/**
* Function for `edit_post_(post_type)` action-hook.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*
* @return void
*/
function wp_kama_edit_post_type_action( $post_id, $post ){
// action...
}
- $post_id(integer)
- ID of the updated post.
- $post(WP_Post)
Post object. To receive it in the callback, pass
2as the fourth argument toadd_action().When called from wp_update_comment_count_now(), the object was loaded before the count was updated, so
$post->comment_countcontains the previous value.To get the current data, retrieve the post again with
get_post( $post_id ). If you need the old and new comment counts, use wp_update_comment_count.
Examples
#1 Clear the book card cache after an update
Suppose book card data is stored in the myplugin_book_{ID} transient. Delete it after a book post is updated so the application regenerates the card on the next request.
add_action( 'edit_post_book', 'myplugin_clear_book_cache' );
function myplugin_clear_book_cache( $post_id ) {
delete_transient( 'myplugin_book_' . $post_id );
}Changelog
| Since 5.1.0 | Introduced. |
Where the hook is called
do_action( "edit_post_{$post->post_type}", $post_id, $post );
do_action( "edit_post_{$post->post_type}", $post->ID, $post );
do_action( "edit_post_{$post->post_type}", $post->ID, $post );
do_action( "edit_post_{$post->post_type}", $post_id, $post );