do_action( "save_post_{$post->post_type}", int $post_id, WP_Post $post, bool $update )

Fires once a post has been saved.


Description

The dynamic portion of the hook name, $post->post_type, refers to the post type slug.

Possible hook names include:

  • save_post_post
  • save_post_page

Top ↑

Parameters

$post_id int
Post ID.
$post WP_Post
Post object.
$update bool
Whether this is an existing post being updated.

Top ↑

Source

File: wp-includes/post.php. View all references

do_action( "save_post_{$post->post_type}", $post_id, $post, $update );


Top ↑

Changelog

Changelog
Version Description
3.7.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Andrew Dixon

    The hook also fires when clicking on the “Add New” option for the custom post type, as it creates the draft in the DB, and when calling the wp_update_post function as well.

  2. Skip to note 2 content
    Contributed by squarecandy

    It’s kind of sad that this more explicit action fires before the less explicit ‘save_post’ action.

    This means if a 3rd party plugin does something using ‘save_post’ you need to use ‘save_post’ to override it too.

    // someone else's code...
    add_action( 'save_post', 'wpdocs_do_something_custom', 15 );
    
    // try to override with CPT action - DOES NOT WORK
    add_action( 'save_post_third_party_cpt', 'wpdocs_override_something_custom', 20, 2 );
    
    // you must do this to override
    add_action( 'save_post', 'wpdocs_override_something_custom', 20, 2 );
    function override_something_custom( $post_id, $post ) {
    	// bail out if this is an autosave
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    		return;
    	}
    
    	// bail out if this is not an event item
    	if ( 'third_party_cpt' !== $post->post_type ) {
    		return;
    	}
    
    	// ... do override stuff
    }
  3. Skip to note 3 content
    Contributed by Nazmul Ahsan

    Notify your newsletter subscribers when a new book (custom post) is added-

    add_action( 'save_post_book', 'wpdocs_notify_subscribers', 10, 3 );
    
    function wpdocs_notify_subscribers( $post_id, $post, $update ) {
    
    	// If an old book is being updated, exit
    	if ( $update ) {
    		return;
    	}
    
    	$subscribers = array( 'john@doe.com', 'jane@doe.com', 'someone@else.com' ); // list of your subscribers
    	$subject     = 'A new book has beed added!';
    	$message     = sprintf( 'We\'ve added a new book, %s. Click <a href="%s">here</a> to see the book', get_the_title( $post ), get_permalink( $post ) );
    
    	wp_mail( $subscribers, $subject, $message );
    }

You must log in before being able to contribute a note or feedback.