Home Forums General Issues update_field on Publish


update_field on Publish

  • I am having trouble with my custom field getting overwritten/blanked. This is the contents to my functions.php in a child theme.

    function get_doi( $new_status, $old_status, $post ) {
        if ( $new_status == 'publish' && $old_status != 'publish' ) {
            $value = "1234";
            update_field( "doi", $value, $post->ID );
    add_action('transition_post_status', 'get_doi', 10000, 3 );

    Only the administrator can Publish, and after I hit Publish the “doi” field is blank on the content.php page.

    I found that if I subtract 1 from postID in update_field
    update_field( "doi", $value, ($post->ID)-1 );
    the last post successfully get ‘1234’. So it must be getting overwritten by the current posts empty doi.

  • I figured it out. I need to use $field_key instead of $field_name.
    update_field( "field_53362544f0012", $value, $post->ID );

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘update_field on Publish’ is closed to new replies.