You can achieve this by using the wp_insert_post hook, which is called before the post is saved or updated. Inside this hook, you can check if the post ID exists to determine if it’s an update or a new post. Here’s how you can do it:
add_action('wp_insert_post', function ($post_ID, $post, $update) {
if (!$update) {
// This is a new post
add_action('save_post', function ($post_ID, $post) {
// Do something when it's a new post
}, 11, 2);
} else {
// This is an update
add_action('save_post', function ($post_ID, $post) {
// Do something when it's an update
}, 9, 2);
}
}, 10, 3);
Related Posts:
- Check for update vs new post on save_post action
- How to avoid infinite loop in save_post callback
- $update is always true in save_post
- Action hook ‘save_post’ triggered when deleting posts
- get post meta before it is updated (during SAVE_POST)
- save_post action firing before I publish / save the post
- save_post action only when creating a new post
- Check if value has changed on save_post
- Minimum Word Count Before A Post Can Be Made Pending Review
- update_post_meta() not working in save_post
- How do I save each option in a multiple select menu as it’s own meta_key + meta_value pair?
- Update post on save
- Post meta checkbox becomes unchecked occasionally
- Return code from save_post action?
- update_post_meta not working in save_post
- save_post only saves meta data on second save
- Get Post ID with insert/edit link
- How to manage saving custom field from Quick edit and Post Save using save_post action hook without colliding each other?
- Why save_post_$(custom_post_type) is fired even if I am not already saving a post?
- Set the value of custom field when post is published or updated
- pass error to admin_notices on “quick edit”/save_post action
- save_post requiere at least one uploaded file to be published
- Get $_POST & $_REQUEST values before adding/updating post
- How can I create a new user account while creating a new post, and then set that new user as the author of that new post?
- Validate post checking if child term is assigned
- Clear cache on post of one type when something happens to post of other type
- Why not fire the save_post event?
- Compare custom taxonomies of updated post (or new post) [Updated with progress]
- WordPress 4.9.8 doesn’t save if content has style=”
- Reset all transients on post or page save
- Using PODS data with save_post
- Cant’t delete my custom posts
- where does this $post_id come from?
- Update current WP post every 3 minutes [closed]
- Changing new post to “pending” on publish – but “Publish failed” – why?
- update_post_meta not working in foreach
- Save post in another table
- Strange bug on post/page save
- draft_to_pending action doesn’t seem to fire
- What action to use for when a post is saved / published, with a caveat
- Custom wp_editor doesn’t update post_content
- Save custom value to main content of post
- Check for page template on save_post hook
- Access NEW/UPDATED post values in save_post() callback function
- save button grayed out randomly
- Post editor doesn’t save embed media [closed]
- How to get a post meta value and pass that to update_option when a post is created or updated?
- WordPress save meta data : Displaying the word Array when selected
- Save current post using custom save button
- Connection Reset on post/page save
- Meta data not saved on save_post
- Insert/Update DB table when making new post or update older one
- Prevent Page/Post From Being Created based on Pages/Posts per User per Time Unit
- How to debug | Some times the “Save Draft” button seems to spin but does not actually save
- My custom title gets duplicated at every save or post update
- Use save_post to generate file on wordpress
- Detect, if post is saved manually or programmatically in save_post-hook
- Why does save_post action fire when creating a new post?
- Return $post_id when DOING_AUTOSAVE?
- Why won’t my metabox data save?
- How to force function to run as the last one when saving the post?
- Disallow a user to post in certain categories
- Get updated meta data after save_post hook
- Manipulating post meta in the customizer
- Non-hierarchical custom taxonomy using checkboxes on edit-screen -> saving issue
- Update Custom Field on Imported Post Creation
- How to auto-update child posts whenever the parent post is updated?
- Remove empty lines ( ) when author updates their post
- How to run a function once on every post in database?
- WordPress Gutenberg get page template value when post updated?
- get_terms on save_post hook
- Trigger “unsaved changes” dialog for custom post meta changes
- Support auto-save and revisions for custom fields
- WordPress turns HTML code to HTML entities
- Updating the database with advanced custom fields
- ACF (Advanced Custom Fields) not updating post or postmeta values
- How to inform the user that the save was not successful?
- Update post meta using save_post hook in multisite
- Create post revision on slug change
- Custom Post Type Metadata Not Saving
- What do the numbers mean at the end of add_action(‘save_post’)…?
- update_post_meta() not saving data inside of save-post filter
- How can I edit comment meta value before it is saved?
- How to get changed post title in my custom plug-in which fires when ‘save_post’ is called?
- Custom Post Type Meta Box Text Input Field Won’t Save When Blank
- troubles with get_post_meta (and saving it)
- save_post trigerred twice
- Updating failed. The response is not a valid JSON response. specific to my browser when I include javascript in my html
- Bookmarking custom template pages which all have the same page ID
- Is there a way to get rendered html content of a WP post after updating?
- Get Category in save_post Hook
- Save data of select list after clicking save
- About WP’s save_post action
- Disable a function from executing in post.php
- does acf_save_post cause endless loop?
- Auto save title as custom meta field value
- Taxonomy Child Not Updating Unless I Click Update Twice
- How do I set a custom post type Category after import using wp_set_post_terms
- A way to see real changes of posts inside an action?
- Save_Post change Custom Post Type Post title to post id number