Which WordPress Hook Fires After Save All Post Data and Post Meta?
When building functionality that triggers after a custom post type update, the immediate instinct is often to rely on save_post. This hook fires when a post is published or updated in the database. However, if you are relying on it to send notifications immediately after saving data to fields like vc_subject or vc_users, you will encounter a specific timing issue: the $post object passed to the callback has not yet been fully populated with the new values.
In the scenario described, the developer is using CMB2 for custom meta fields and attempting to send emails based on those fields. The code registers an action on save_post, but inside the function, accessing $crm->vc_all_vc or similar meta keys returns stale data because the database write cycle hasn't completed yet at that exact moment in the hook lifecycle.
Understanding the Hook Lifecycle
The core issue lies in how WordPress processes a post save request. When an admin clicks "Update," WordPress executes several steps:
- Validation: Input sanitization and validation occur.
- Database Write: The row in the
wp_poststable is updated, and custom meta rows are inserted or modified. - Hook Execution: The
save_post_{post_type}hook fires (or simplysave_post).
Crucially, during the execution of the save_post action, WordPress has just finished writing the data to the database but hasn't necessarily flushed all cache layers or fully resolved the post object properties in memory before passing it to your callback. While the Codex states save_post fires after the post is saved, the specific meta values accessed via $post->meta_key might still reflect the state prior to the final commit in certain contexts, or more commonly, you are fighting against the order of execution where other plugins might be modifying data after your hook runs.
In the provided code example, the function send_mail_to_user attempts to read $crm->vc_subject. If this property is a post meta field managed by CMB2, accessing it directly on the WP_Post object before the meta cache is fully synchronized can lead to reading the old value. This results in emails being sent with outdated subjects or failing to identify new users added via vc_users.
The Solution: Using edit_post
To reliably access the new data immediately after a save, you should switch to the edit_post action hook. Unlike save_post, which is primarily designed for actions that need to happen regardless of whether the post was actually changed (and often fires even if nothing changed), edit_post specifically indicates that the post has been updated in the database with new content.
The signature for edit_post is similar, but the timing is slightly more favorable for reading fresh meta data:
add_action( 'edit_post', 'send_mail_to_user' );
// Optional: You might still want to pass ID and $post if needed, though edit_post usually accepts just $post.
However, there is a critical nuance. The edit_post hook fires after the post data has been saved to the database. When you access $post->ID, $post->post_title, or any meta keys via get_post_meta() inside this callback, you are guaranteed to be reading the values that were just written.
Handling the "No Change" Scenario
A common pitfall when switching from save_post to edit_post is that edit_post does not fire if the post content has not actually changed (e.g., clicking "Update" without modifying anything). If your requirement is to send an email whenever a CRM record is touched, whether it changes or not, you must wrap your logic in a check.
You can compare the old and new values. Since edit_post runs after the save, you can safely use get_post_meta() to read the current state of the database:
function send_mail_to_user( $post_id ) {
// Get the post object immediately; it reflects the saved state
$crm = get_post( $post_id );
// Optional: Check if actual content changed to avoid spamming emails on "Update" clicks
// Note: This requires access to old values or logic outside this hook context.
if ( ! empty( $crm->vc_all_vc ) ) {
// Proceed with email logic using the fresh $crm object
// ... existing email sending logic ...
}
}
add_action( 'edit_post', 'send_mail_to_user' );
Trade-offs and Performance Considerations
While edit_post solves the data availability problem, it introduces a slight performance overhead. Because this hook fires after every save action, you must be careful with how much processing you do inside the callback. In the example code, querying users via WP_User_Query, iterating through groups, and looping through email lists is computationally expensive. Running these queries on every single edit (even trivial ones) can slow down the admin dashboard significantly.
If the cost of the email logic is high, consider decoupling it. You could use a flag stored in post meta during the save process to indicate that an email needs sending, and then let a separate background job or a higher-priority scheduler handle the actual dispatch. Alternatively, ensure your queries are optimized and only run when necessary.
Final Recommendation
For retrieving the most up-to-date post data and meta immediately after a save operation, edit_post is the correct hook to use. It ensures that the $post object or any get_*() functions called within it reflect the committed database state. Just remember to account for scenarios where no content changes to prevent unnecessary processing. If you are working in an environment where strict timing is required and need access to both old and new values simultaneously, you might consider using do_action_reflow or checking the $post object against a pre-save snapshot, but for standard notification tasks like sending emails after a CRM update, edit_post provides the necessary data integrity.