Support

Account

Home Forums General Issues update_field on Publish

Solving

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.

    <?php
    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 );
    ?>

    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( “field_534dc7420db07”, $value, ($post->ID)-1 );
    the last post successfully gets ’12345′. So it must be getting overwritten by the current posts empty doi.

    * I thought I had this figured out but I deleted my Custom Fields and created new ones, and now its broken.

  • Hi,
    I had a similare issue where when i substracted 1 to the post id it was working and with the current post ID it wasen’t.
    In my case it was caused by the fact that at the point of updating the field value, the values wasen’t saved yet.

    I am not sure if i understand what you are trying to achive but it looks like you want to register a value to the posts that wasen’t published ang got changed to publish.

    If you are trying to do that for new post and want the function to run only when post is created you could use

    // run before ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);

    inside your my_acf_save_post you could check if field_534dc7420db07 is set.
    If it is it will mean you are juste updating the post if it dosen’t it means it’s a new post and you can update the field value.

    If you are trying to run your function when a post pass from private to published
    have you tried to call private_to_published action?

    hope it helps

  • Right when the post is Published by the Administrator, I need to generate a unique ID for that post (this is done by the server). So the Contributor does not (and shouldn’t) enter anything.

    Isn’t the action private_to_published basically the same as transition_post_status?

    PS I also just realized WordPress has recently been auto-updated, not sure if this is causing it.

  • I switched it all to:

    function your_callback( $post ) {
            $value = "12345";
            update_field( "field_534dc7420db07", $value, $post->ID );
    }
    add_action( 'pending_to_publish', 'your_callback' );

    but it has the same effect as before. The current post field gets blanked out, but if I use postid-1 it works fine.

  • I found that if I comment out line 61 of acf.php it works (however all my other Custom Fields do not get saved).
    //add_action('acf/save_post', array($this, 'save_post'), 10);

  • Hi,
    I had a look at the transition_post_status action on the codec, looks like it’s “not intended for use by plugin and theme developers”.

    if you $value is a static value I think what you want is pretty straight forward with ACF.

    // run AFTER ACF saves the $_POST['fields'] data
    // here we run the acf/save_post AFTER the data are saved and we update the field to your $value
    
    add_action('acf/save_post', 'my_acf_save_post', 20);	
     
    function my_acf_save_post( $post_id ){
      
        $value = "12345";
        update_field('field_534dc7420db07', $new_tax, $post_id);
    }
    

    That way you ensure that the $value stays always the same, it can be on newly published posts or on post that goes from pending to published, as soon as the post is saves the field_534dc7420db07 gets your value after it was saved.

    you should leave the acf.php as it was and shouldn’t comment anything on that part it will end up breaking things.

    then if you want to make more complicated stuff you can check if the post is published or pending or any other stuff using it’s id that is stored in $post_id

  • Hooking the acf/save_post wont work, since every time the post is updated after it is published the acf/save_post will trigger and replace the original $value with a new $value.

    Consider $value being a unique timestamp that is run only once when the post transitions to the published state. $value = time();

    If this is my functions.php, when the already Published post is updated field_534dc7420db07 gets a new value.

    add_action('acf/save_post', 'my_acf_save_post', 20);
    function my_acf_save_post( $post_id ){
        $stat = get_post_status( $post_id );
        $value = time();
        update_field('field_534dc7420db07', $value, $post_id);
    }

    I can check the status of the post in my_acf_save_post, but only the current value, not if it is transitioning from pending to publish.

    This is the reason I need to, and should, run the transition_post_status.

  • Does acf/save_post runs when posts are pending?
    if it doesn’t you could check if your field_534dc7420db07 has a value and update it with $value only if it doesn’t. if acf/save_post runs before the values are stored in the database an “updated” post will already have a value for that field and wont get updated.

    would that work?

  • 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.

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

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