Using WordPress ‘edit_post_{$post->post_type}’ PHP action

The edit_post_{$post->post_type} WordPress PHP action fires once an existing post has been updated. The dynamic part of the hook name, $post->post_type, refers to the post type slug. Possible hook names include edit_post_post and edit_post_page.

Usage

add_action('edit_post_post', 'your_custom_function', 10, 2);

function your_custom_function($post_id, $post) {
    // your custom code here
}

Parameters

  • $post_id (int): The post ID.
  • $post (WP_Post): The post object.

More information

See WordPress Developer Resources: edit_post_{$post->post_type}

Examples

Update Post Meta

Update the post meta ‘_views’ every time a post is updated.

add_action('edit_post_post', 'update_views_meta', 10, 2);

function update_views_meta($post_id, $post) {
    $views = get_post_meta($post_id, '_views', true);
    $views++;
    update_post_meta($post_id, '_views', $views);
}

Send Email Notification

Send an email notification to the admin when a page is updated.

add_action('edit_post_page', 'send_email_notification', 10, 2);

function send_email_notification($post_id, $post) {
    $to = get_option('admin_email');
    $subject = 'Page Updated: ' . $post->post_title;
    $message = 'The page "' . $post->post_title . '" has been updated.';
    wp_mail($to, $subject, $message);
}

Update Post Slug

Automatically update the post slug based on the post title.

add_action('edit_post_post', 'update_post_slug', 10, 2);

function update_post_slug($post_id, $post) {
    $new_slug = sanitize_title($post->post_title);
    wp_update_post(array('ID' => $post_id, 'post_name' => $new_slug));
}

Sync Custom Taxonomy

Sync a custom taxonomy term with the post’s category.

add_action('edit_post_post', 'sync_custom_taxonomy', 10, 2);

function sync_custom_taxonomy($post_id, $post) {
    $categories = wp_get_post_categories($post_id);
    wp_set_object_terms($post_id, $categories, 'your_custom_taxonomy');
}

Log Post Updates

Log post updates to a custom log file.

add_action('edit_post_post', 'log_post_updates', 10, 2);
function log_post_updates($post_id, $post) {
$log_file = WP_CONTENT_DIR . '/post-updates.log';
$log_entry = date('Y-m-d H:i:s') . ' - Post ID: ' . $post_id . ' - Title: ' . $post->post_title . "\n";
file_put_contents($log_file, $log_entry, FILE_APPEND);
}