When does save_post hook fire on post save/update

by Krzysztof Chodera   Last Updated July 10, 2019 11:08 AM

In my plugin I want to update sitemap every time page or post is created/modified. To achieve this I use save_post hook:

add_action( 'save_post', 'update_sitemap', 10, 3);

Upon creating/saving/updating/deleting any page my callback method update_sitemap is fired but when i create/save/update/delete any post it doesn't seem to fire callback update_sitemap immediately.

I tested this for custom post types and it works immediately like for pages.

When I add 2 new regular posts, callback method is called once. Only after any further modification callback for 2nd post is fired.

Is this expected behaviour for save_post hook?

Answers 1

save_post is fired at the end of wp_insert_post() which is the core function that's run whenever a post is inserted or updated (wp_update_post() calls it internally). This includes when the post is updated via the classic editor and the block editor (Gutenberg), as well whenever it's updated via the REST API. The only reason it wouldn't fire is if the post was being updated via SQL directly (via a plugin or otherwise), or when only post meta is updated via a function.

So no, this is not the expected behaviour. If your function isn't firing then it could be interference from another theme or plugin, or it could be an issue with the function itself, but there's not enough information in the question to say either way.

Jacob Peattie
Jacob Peattie
July 10, 2019 10:49 AM

Related Questions

admin_post hook error message passing

Updated April 09, 2015 01:03 AM

Does WordPress have something like timer hook?

Updated July 13, 2016 08:03 AM

remove_action of lower priority inside add_action

Updated September 05, 2016 08:05 AM