Why is save_post triggered even when I havent saved the post

See Why does save_post action fire when creating a new post?

You’re getting the error as you need to check for the existence of $_POST['xxx'], rather than just checking if $_POST is set (it will always be set & an empty array by default).

add_action('save_post', function($id) {
    if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || !isset( $_POST['xxx'] ) )
        return;

    update_post_meta( $id, 'xxx', sprintf( '%f', $_POST['xxx'] ) );
    // run further code
});

Leave a Comment