Access NEW/UPDATED post values in save_post() callback function

Question

I want to run a “background” function when a custom post type is saved (updated or created) using the NEW/UPDATED post values. The “background” function updates a taxonomy, based on a custom field in the custom post type.

How do I access the NEW/UPDATED post values? I’m obviously doing something dumb here but I’ve been at it two hours with no progress.

I thought save_post() was fired after the dbase updated but I only get “old” values.

For the avoidance of doubt, I am def calling the functions correctly:

function custom_post_type_callback($post_id) {
  //obvs the background function isn't defined here my actual code
  my_background_function($post_id){
     $my_variable = get_post($post_id); // <- this is always the "old" values
  };
}

add_action( 'save_post_custom_post_type', 'custom_post_type_callback' );

It works perfectly if I save the post AGAIN.

0
dtw 2 months 2022-06-12T07:20:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse