Support

Account

Home Forums Backend Issues (wp-admin) The updated field is not refreshed

Solving

The updated field is not refreshed

  • Hello,

    After a custom post type is saved, I hook on the action “acf/save_post” so I can create a category with the post name, then I update a taxonomy field of the custom post type with the id of this category.
    When I clic on publish the post, the taxonomy field ( a select) is still empty. But If I check the database, it is filled correctly. So I refresh the page, and now the taxonomy field is displaying the correct value in the dropdown.

    My code in functions.php :

    add_action('acf/save_post', 'save_commune');
    function save_commune( $post_id ) {
        global $post; 
        $cat_id = wp_create_category( $post->post_title, 0 );
        update_field('cat_id', $cat_id, $post_id );
    }

    Any ideas on how I can see a correctly displayed field after publishing a post without refreshing the whole page manually ?

  • When updating a field that does not already have a value set you must use the field key instead of the field name. This is explained in the second section of this page https://www.advancedcustomfields.com/resources/update_field/

  • So I’ve changed my code :

    add_action('acf/save_post', 'save_commune');
    function save_commune( $post_id ) {
        global $post; 
        $cat_id = wp_create_category( $post->post_title, 0 );
        $field = get_field_object('cat_id');
        update_field($field['key'], $cat_id, $post_id );
    }

    But unfortunately when I save the post with a title. It still shows an empty select for my Taxonomy field. I need to manually refresh the page to see the selected value.

  • This results in no field object being returned if the field has no entry in the DB for the current post.

    
    $field = get_field_object('cat_id');
    

    Even if you do it this way

    
    $field = get_field_object('cat_id', $post_id);
    

    ACF must find the field key reference in the DB for this field on this post. Since there is nothing in the DB ACF cannot get the field key reference and this means it cannot find the correct field definition. You must supply the field key.

  • I’m also having this issue with the ACF field not being updated in the backend (without full page refresh) using update_field($field_key, $field_value, $post_id) during ‘acf/save_post.’

    
        public function register_hooks()
        {
            // add_action('acf/save_post', [$this, 'acf_save_post'], 5);
            add_action('acf/save_post', [$this, 'acf_save_post'], 20);
        }
        public function acf_save_post($post_id)
        {
            $post_type = get_post_type($post_id);
            switch ($post_type) {
                case 'event':
                    $this->acf_save_post_event($post_id);
            }
        }
        public function acf_save_post_event($post_id)
        {
            $field_key = 'field_6247dc975b487';
            $new_value = 'hello';
    
            if ($new_value) {
    
                //$_POST['acf'][$field_key] = $new_value;
                update_field($field_key, $new_value, $post_id);
    
            } else {
                //$_POST['acf'][$field_key] = '';
                delete_field($field_key, $post_id);
    
            }
        }
    

    I am using the field key rather than the field name. I attempted the methods under “Applied after save” and “Applied before save” at href=”https://www.advancedcustomfields.com/resources/acf-save_post/. This is top-level field, not under a group or repeater. It is a jQuery datepicker field, by I also attempted this with a plain text field, with the same results. The field updates properly in the database, but this isn’t reflected in the post edit screen until a full page refresh. The Gutenberg editor is active for the post type edit, so the page does not fully refresh on “Update.”

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

You must be logged in to reply to this topic.