Retrive acf fields in publish_post action for wp_mail

…Removed my prior ‘solutions’…


So, the real problem here is that this function is hooked to publish_post action, at which point the post might not be saved yet, if it is a new post. Only if it is a post already saved but with some other status, we can access the ACF values using get_field().

Solution could be to use a hook acf/save_post with priority of 10 or larger, since it fires after the post has been saved, therefore allowing us to get the values using get_field().

This is a short example of usage of this hook:

function my_acf_save_post( $post_id ) {

    // bail if wrong post type
    if( get_post_type( $post_id ) !== 'envios' ) {
         return;
    }

    // get new value
    $posts = get_field( 'casos', $post_id );


    // do something

}

add_action('acf/save_post', 'my_acf_save_post', 20);