I have a problem, I want to update a custom user meta data field using another custom post field. It has to happen AFTER the whole post INCLUDING CUSTOM FIELDS saves / updates. But all the calls I find: update the basic post data, then call the action, THEN update the custom fields.
saved_post & post_updated cause this code to be delayed by 1 saved. ie, if I were to make a new post and set the $my_value to 5, first time I saved it would come back with 0, then the next time it would come back with 5. ect ect.
Does anyone know of an action hook that runs after custom post data has been saved? or how to make save_post or post_updated to fire after the custom post data?
function zhu_li_do_the_thing( $post_id ) {
//get post data, extract author-ID and post-Type
$post = get_post( $post_id );
$post_type = $post->post_type;
$userid = $post->post_author;
//if your my custom post type, then get the custom field value i want.
if( 'my_custom_post_type' == $post_type ){
$post_custom_fields = get_post_custom($post_id);
$my_custom_field = $custom_fields['im_a_field'];
foreach($im_a_field as $key ){
$my_value = $key;
}
// if the value starts with a “+” or “-” do math with it against authors-custom-metadata, if it’s null ignore it, if it’s anything else, make the metadata = the value.
if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){
$my_int = intval($my_value);
$author_int = intval(get_user_meta($userid, 'the_objective, true));
$result = ($author_int + $str);
update_user_meta( $userid, 'the_objective', $result );
}elseif($my_value == null ){
return;
}else{
update_user_meta( $userid, 'the_objective', $my_value )
}}};
add_action( 'save_post, 'zhu_li_do_the_thing' );
You should use
pre_post_update
to check the post meta before and usepost_updated
to check the updated post meta.The following is a snippet of code from a class that checks the stock status of a woocomerce product.