Support

Account

Home Forums General Issues Update Field Programmatically

Solved

Update Field Programmatically

  • Good day.
    I am trying to update fields programmatically using the the code below.

    
    <?php
    function update_product_info() {
        if ( ! wp_is_post_revision( $post_id ) ){
            $product_args = array(
                'post_title'   => $product_name,
                'post_name'    => $product_name,
                'post_content' => $product_description 
            );
    
            // ACF Product Price
            $field_key = "field_535e1cd4915dc";
            $value = $product_price;
    
            // unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'update_product_info');
            // update the post, which calls save_post again
            wp_update_post( $product_args );
            // update ACF Price Field
            update_field( $field_key, $value, $post_id );
            // re-hook this function
            add_action('save_post', 'update_product_info');
        }	
    }
    add_action( 'save_post', 'update_product_info' );
    

    Basically what this does is populates the post title and content upon save to which it works great. But I’m trying also to update my ACF fields using their field keys as suggested, but just doesn’t work as I expect it.
    (Of course all variables are declared, just omitted some to keep the code shorter.)

    I might be missing something out. Any help is appreciated.
    Thanks in advance!

  • Nevermind. Finally figured it out.
    Replaced it with acf/save_post instead and now works as it should.

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

The topic ‘Update Field Programmatically’ is closed to new replies.