saved_post post_updated firing before custom fields are updated, what function fires after custom fields are updated? wordpress

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.

Read More

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' );

Related posts

Leave a Reply

1 comment

  1. You should use pre_post_update to check the post meta before and use post_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.

    function __construct()
    {   
        add_action( 'post_updated', array( $this, 'post_updated' ), 1, 1 );
        add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 1, 1 );
    }
    
    function pre_post_update( $post_ID )
    {       
        global $post_type;
    
        if( $post_type == 'product' )
            define( 'stock_status_previous', get_post_meta( $post_ID, '_stock_status', 1 ) );
    }
    
    function post_updated( $post_ID )
    {
        if( defined( 'stock_status_previous' ) )
        {
            $stock_status = get_post_meta( $post_ID, '_stock_status', 1 );
    
            if( $stock_status == stock_status_check )
                return;
    
            // post meta is different - sp do something
    
    }