Support

Account

Home Forums Add-ons Repeater Field Update sub field before/after save post Reply To: Update sub field before/after save post

  • I had a very similar problem. I needed to compare both the previous and newly submitted value (of type array), whilst also updating the post after AFC made its changes. The goal was to create a computed/ calculated/ lookup/ rollup field for backlinking/ bidirectional linking purposes

    Here’s the code to achieve this.

    Note that you need two actions, the first one runs before the post is saved and stores the old data in a global variable.

    The second action than get the newly submitted form data and updates the desired fields. In below code the old and new values are compared and the update is only executed if the new value is different.

    
    
    // Global variable to store the old_field_value before the post is updated
    global $old_field_value;
    
    // Use the acf/validate_save_post action to get the old_field_value before the post is updated
    add_action('acf/validate_save_post', 'store_old_field_value', 10);
    
    function store_old_field_value() {
        global $old_field_value;
        $post_id = $_POST['post_ID'];
    
        if (get_post_type($post_id) === 'your_post_type') {
            $old_field_value = get_post_meta($post_id, 'your_field_name', true);
        }
    }
    
    // Automatically save related posts based on the linked posts and their associated posts
    add_action('acf/save_post', 'update_related_posts', 20);
    
    function update_related_posts($post_id) {
        global $old_field_value;
    
        // Check if the save is an auto-save, in which case we don't want to run this function
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
    
        // Check if the current user has permission to edit the post
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    
        // Check if the saved post is of the desired post type
        if (get_post_type($post_id) !== 'your_post_type') {
            return;
        }
    
        // Get submitted values.
        $new_values = $_POST['acf'];
    
        // Important add your 'your_field_key' (starting with "field_"), not the 'your_field_name'
        $new_field_value = is_array($new_values['your_field_key']) ? $new_values['your_field_key'] : array();
    
        // Check if the field has changed
        if ($old_field_value != $new_field_value) {
    
            // Initialize the $related_posts array
            $related_posts = array();
    
            // Iterate through the new linked posts and get the associated posts field value
            foreach ($new_field_value as $linked_post_id) {
                // Get the associated posts field value from the linked post using ACF's get_field() function
                $associated_posts = get_field('associated_posts_field_name', $linked_post_id);
    
                // Merge the associated posts with the main $related_posts array
                $related_posts = array_merge($related_posts, $associated_posts);
            }
    
            // Get the existing related posts field value from the current post using ACF's get_field() function
            $existing_related_posts = get_field('related_posts_field_name', $post_id);
            $existing_related_posts_arr = is_array($existing_related_posts) ? $existing_related_posts : array();
    
            // Merge the existing related posts with the new related posts array
            $related_posts = array_merge($related_posts, $existing_related_posts_arr);
    
            // Remove duplicate post IDs
            $related_posts = array_unique($related_posts);
    
            // Update the related posts field in the current post using ACF's update_field() function
            update_field('related_posts_field_name', $related_posts, $post_id);
        }
    }