Support

Account

Home Forums General Issues update_field on Publish Reply To: update_field on Publish

  • Yes acf/save_post runs whenever the post is saved, even as a draft. Because of the nature of $value I cannot just assign a value to every post (lots of spam posts), only those that are accepted for Publish (the $value is a limited id that is taken off a finite stack, so only posts that are Published should be assigned one).

    I cannot check if the field is empty, because ACF does not have a function to hide fields, if the user was to enter anything into the field box, an actual $value would not be assigned.

    I did however find a way to hide the field with a custom css, so that a user cannot enter anything into it (unless they do some css hacking). I then can use the acf/save_post, and if the status is “Publish” AND the field is empty, then I assign a $value.

    This is just a workaround, and ideally ACF should work with:

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

    I believe it is a timing issue, as ACF is set to trigger with priority 10. So I set the get_doi to trigger with priority 10000, so it should trigger after ACF saves, but I does not.