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

Parameters

$post_idint
Post ID.
$postWP_Post
Post object.
$updatebool
Whether this is an existing post being updated.

Source

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

Changelog

VersionDescription
3.7.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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 5 content

    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 6 content

    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.