do_action( 'save_post', int $post_ID , WP_Post $post , bool $update )
Fires once a post has been saved.
Description Description
Parameters Parameters
- $post_ID
-
(int) Post ID.
- $post
-
(WP_Post) Post object.
- $update
-
(bool) Whether this is an existing post being updated or not.
Source Source
File: wp-includes/post.php
Changelog Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
When using WordPress 3.7 or later, it’s a good idea to use the save_post_{$post->post_type} hook when it makes sense to in order to reduce code and fire less hooks overall when posts are created and updated.
Documentation can be found here: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/
Force a new post of have specific category term,
add_action( 'save_post', 'set_post_default_category', 10,3 ); function set_post_default_category( $post_id, $post, $update ) { // Only want to set if this is a new post! if ( $update ){ return; } // Only set for post_type = post! if ( 'post' !== $post->post_type ) { return; } // Get the default term using the slug, its more portable! $term = get_term_by( 'slug', 'my-custom-term', 'category' ); wp_set_post_terms( $post_id, $term->term_id, 'category', true ); }Expand full source codeCollapse full source code