Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Update post meta within save_post action

The problem is probably that the $post_id parameter passed to via the save_post_$post_type hook is not the same $post_id as the original post – it’s the $post_id of the temporary post that’s created when you edit a post.

To confirm this:

add_filter( 'pre_post_update', 'wpse_pre_post_update', 10, 3 );
add_filter( 'save_post', 'wpse_save_post', 10, 1 );

function wpse_pre_post_update( $post_id, $post, $update ) {
  echo $post_id . '<br>';
}
function wpse_save_post( $post_id ) {
  echo $post_id;
  wp_die();
}

If you’re trying to add a new post, then pre_post_update hook doesn’t fire and WP will die before taking you to edit screen. Comment out the wp_die() line, then click the “Add New” link. Now, uncomment out the wp_die() and save the post. Two non-identical IDs should be output before WP dies.

The first ID is the ID of the actual post.
The second ID is the ID of the temporary post.

It doesn’t really make sense to me why WordPress does it this way, and I have no idea if it’s always been like this. But it has this behavior for me running a fresh install of WP 4.7.3.

Unless for some reason you need to update the option after the post is saved, I’d use the pre_post_update hook instead.

add_filter( 'pre_post_update', 'update_promo_tag_id', 10, 3 );
function update_promo_tag_id( $post_id, $post, $update ) { 
  if( 'promo' !== $post->post_type ) {
    return $post_id;
  } 
  update_field( 'one_time_tag_id', 'updated!', $post_id );
  return $post_id;
}

Related Posts:

  1. Better post meta efficiency?
  2. Get the post_id of a new post
  3. How to generate numbers indistinguishable for the IDs of the posts
  4. Get Meta Key Value While Saving Post
  5. Saving custom fields to a custom taxonomy
  6. Change post author without using wp_update_post()
  7. Saving Post Data in Another Database
  8. Run a function when a custom post is update?
  9. Grab meta data before post is saved
  10. Run function after post is updated
  11. How to make internal links creating plugin to respect ACF?
  12. Is there a action hook for the “Empty Trash” button?
  13. How to check if post meta key exists or not in wordpress database
  14. Function to execute when a post is moved to trash .
  15. add action only on post publish – not update
  16. How to add category to: ‘wp-admin/post-new.php’?
  17. How to validate XML-RPC post creation and cancel when needed?
  18. update_post_meta and update_field ony working when saving the post
  19. How can I retrieve multiple get_post_meta values efficiently?
  20. Code to make a post sticky
  21. How to allow hidden custom fields to be added from wp-admin/post.php?
  22. Create posts on user registration
  23. Export WordPress Posts and Meta Information in CSV format
  24. How to publish a post with empty title and empty content?
  25. Delete duplicated wp_postmeta record
  26. flush_rewrite_rules on save_post Does Not Work on First Post Save
  27. How to add a “publish” link to the quick actions
  28. Action hook ‘wp’ firing twice… why?
  29. Will a large postmeta table slow a site down?
  30. Update all posts automatically when using post_meta
  31. What to use: wp_is_post_autosave( $post_id ) or DOING_AUTOSAVE?
  32. How to batch update post content with custom post meta value
  33. Change slug with custom field
  34. Count singular post views automatically
  35. Setting post meta data to random value during post status transition / on publish
  36. How can I pass $post object to ‘save_post’ add_action?
  37. Edit meta data does’t work with custom sql
  38. Human Time Diff, change mins to minutes
  39. get_post_custom stripping styling issue
  40. Change post format using custom field
  41. Have save_post write to database image meta [closed]
  42. Custom function for “Submit for Review” hook
  43. Does WP get all post_meta on POST page?
  44. get post id in while loops outputting page id
  45. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  46. Exporting Data from WordPress into a flat table
  47. how “manage_posts_custom_column” action hook relate to “manage_${post_type}_columns” filter hook?
  48. Adding Multiple Values to a Post Meta Key
  49. Get updated post meta on save_post action?
  50. How to get Advanced Custom Field Value According using POST ID? [closed]
  51. Using radio button meta data from a custom meta box
  52. update_post_meta() whenever custom post type is updated
  53. How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?
  54. publish_post conflicts with save_post
  55. PHP Notice error (when on 404 page)
  56. add_action not using ‘delete_post’ action with wp_delete_post
  57. What Is meta_id In wp_postmeta?
  58. the_posts filter been called multiple time
  59. Get post id in wordpress action?
  60. How can i do something after head like adding a hook for after head but before post
  61. Checking if a post with certain meta value exists
  62. How to run a function when post is edited or updated using publish post action?
  63. How to permanently delete a post meta entry?
  64. Add custom field automatically (add_post_meta) with value based on number of words of article
  65. Create cron job without a plugin?
  66. Modify WP_Post before processing
  67. Set default Custom Post Meta Value
  68. Save re-arranged draggable post items to wordpress database
  69. I would like to give special promotion for the first 100 posts in my blog? Can anyone tell me how to do that?
  70. Change post_date to post_modified date on post template?
  71. Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
  72. Guest Author – How to display posts on /author/ archive page
  73. How to get post bulk edit action trigger and get edited post ids?
  74. How can I sort posts by the date and a custom meta field?
  75. is there a way to show the the post title after the image?
  76. Plugin with action ‘save_post’ needs to press publish twice on order to publish
  77. How do I correctly set up a WP-Query to only show upcoming event-posts?
  78. Adding custom fields to bbpress reply form
  79. Check if checkbox is marked on publish/update post
  80. Adding buttons to Add New Post and Add New Page
  81. ACF (Advanced Custom Fields) not updating post or postmeta values
  82. Force the “Choose from the most used tags” meta box section to always be expanded
  83. Meta value does not save for scheduled posts
  84. Custom Posts Query and meta_query Sort Order
  85. Query values from a Post Object Repeater [closed]
  86. How to access $post from a callback function
  87. Updating post data on save (save_post vs wp_insert_post_data)
  88. How to compare two posts including their meta fields on a scalable base?
  89. Getting value from get_post_custom
  90. Calling Different Custom Post Timestamps in a table
  91. Is it possible to paste a link without tags and make it directly a link in a post?
  92. Set static page/post from another blog on same network
  93. Cannot retrieve a custom RSS field from posts
  94. Copy post to separate database with “add_action(….)”
  95. How to automate featured posts number? [duplicate]
  96. Call Web Services on post first publish
  97. How to check if single.php has already called the_post_thumbnail function
  98. Customize rel=canonical tag for single blog post
  99. Change all author links in Blog roll
  100. How to I retrieve the ID from the Posts page?
Categories posts Tags actions, advanced-custom-fields, post-meta, posts, save-post
How can I port forward with iptables?
How do you install Node.JS on CentOS?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress