Support

Account

Home Forums Bug Reports acf/save_post firing before the ACF fields get saved

Helping

acf/save_post firing before the ACF fields get saved

    • motocom

    • December 17, 2019 at 12:24 am

    Strange problem here. I’m running exactly same code on local dev and on my server.

    So I am trying update some of the post object with ACF field data from my post when the user initially saves a post.

    Using ACF Save Post

    
    <?php
        
        class Product {
        
            public function __construct() {
        
                // process product details when saving the post
                add_action('acf/save_post', [ $this, 'acf_save_product' ], 20);
        
            }
       
            /**
             * @param int $post_id
             * @return void
             */
            public function acf_save_product ($post_id) {
        
                // check we are on the correct object
                if(get_post_type($post_id) <> 'product') return;
        
                var_dump($post_id);
                var_dump( get_field('product_vehicle', $post_id) );
                exit;
                
            }
        
        } new Product();
    
    ?>
    

    So on my local development area ([docker][2]) this is whats returned on initial create/save post. Brand new post and I have selected choices from product_vehicle ACF field.

    
        int(282)
        Array
        (
            [0] => 34
            [1] => 35
            [2] => 36
            [3] => 37
            [4] => 38
        )
    

    When i perform the exact same test on the the staging server; same fields, same choices, ACF fields synced using ACF json etc. This is what is returned on the initial create/save post.

    
        int(145)
    

    As you can see it is not getting the saved ACF field data. Its the same for all my ACF fields I’m trying to capture. Returning false on all of them.

    But when I save the post for the second time it then returns the ACF field data like it should on the first save.

    
        int(145)
        Array
        (
            [0] => 34
            [1] => 35
            [2] => 36
            [3] => 37
            [4] => 38
        )
    

    It’s really difficult to debug this when it works on local dev on the first save, but only on the second save on the staging server.

    It appears acf/save_post to be firing before the ACF fields get saved on every save.

    So it’s only till the second save that it gets the newly added/changed ACF data.

    I’ve tried wacking the priority up to a 100 and still not firing late enough.

    Any ideas would be great thanks.

  • a priority of > 10 happens after the values are saved to the DB, 11 will do it.

    I would suspect some type of caching of the values is going on where it does not seem to be working.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.