Support

Account

Home Forums Add-ons Repeater Field Add new Row if Field Value changed

Solving

Add new Row if Field Value changed

  • Hello, everyone,

    following scenario.

    I use ACF to add other fields to users such as weight.

    The weight is in a number field.

    Now I would like to expand the whole thing with a kind of logbook function.

    As soon as the “Weight” value changes (front-end entry), a new entry should automatically be created in the repeater field together with the current date.

    I had the following idea, which unfortunately doesn’t work (I’m a beginner)

    
    add_action('acf/save_post', 'my_acf_save_post', 5);
    
    function my_acf_save_post() {
    
    	$user_id = get_current_user_id();
    	$user_body_weight = get_field('bodyweight', 'user_'.$user_id);
    	$current_date = date('Ymd');
    	
        // Get previous values.
        $prev_values = get_fields( $post_id );
    
        // Get submitted values.
        $values = $_POST['acf'];
    
        // Check if a specific value was updated.
        if( isset($_POST['acf']['field_615c365c8ba57']) ) {
    
            // Do something.
    
    		$row = array(
    				'field_615acd3d04123'   => $user_body_weight,
    				'field_615acd4904124'   => $current_date
    			);
           	add_row(' field_615acd90fb1e4', $row);
    
        }
    }
    

    Can someone help?

    Many greetings
    Johnny

  • Is the repeater shown on the front end along with the weight input field?

  • Thanks for the fast respond.

    Both.

    There is a page where only the weight input field and a profile page with repeater and weight input.

  • If both the input field and the repeater are shown then you cannot use add_row().

    The reason for this is that with a priority of <10 ACF has not yet saved new values. When it does it saves the values that have been submitted in the repeater, overwriting the changes you made.

    To add a row to the repeater you must alter the array in $POST['acf']['field_615acd90fb1e4'] to add the new row so that ACF can update the values.

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

You must be logged in to reply to this topic.