Home Forums General Issues saving different value than $_POST['acf']['field_xxx']


saving different value than $_POST['acf']['field_xxx']

  • Hi,
    I am having a problem saving data that is not the same as the _Post,
    It is writing what is coming from the form, And I want it to write the new information.

    I got some of this from here update_value-hook-not-working-when-saving-post/
    as I think it is not using the updated value but the value from the form.

    This is the code I have and I cannot not figure out where I have gone wrong.

    add_action('acf/save_post', 's4f_save_post_step_content', 20);
    function s4f_save_post_step_content( $post_id ) {
        if (wp_is_post_revision($post_id)) {
            return false;
        if ( (isset($_POST['acf'])) && (get_post_type($post_id) == 'step') ) { 
            $fields = $_POST['acf'];
         //  Data to retrieve and calculate
         	$goal_post_id = $fields['field_6052515cbb6c7'];  // an array related_goal 
         	$the_goal_post_id = $goal_post_id[0];  // related_goal  
         // data from the related goal
         	$goal_collections_not_from_s4f = get_field('field_605257355dc6d', $the_goal_post_id); 
         	$goal_collections_from_s4f = get_field('field_605256cf5dc6c', $the_goal_post_id); 
         	$goal_total = $fields['field_605254421e35d'];  
    	// data from step input form
         	$step_old_collected_to_date_not_s4f = $fields['field_605254a91e35e']; // used to compare with goal to see if it needs updating
         	$new_step_input_not_from_s4f = $fields['field_605260f5663f1'];  // new_collected_not_s4f  input
         	$new_calc_collected_from_other_sources = $goal_collections_not_from_s4f + $new_step_input_not_from_s4f; // recalculated here in case this is an update
         	$new_calc_total_collected = $new_calc_collected_from_other_sources + $goal_collections_from_s4f;  // grand total recalculated here in case this is an update
        if ($goal_collections_not_from_s4f !== $step_old_collected_to_date_not_s4f):
            if (isset($_POST['acf']['field_605254a91e35e'])) : // replace with your field key
                 $_POST['acf']['field_605254a91e35e'] = $goal_collections_not_from_s4f;
               else :  update_field('collected_to_date_not_s4f',$goal_collections_not_from_s4f,$post_id);
            if (isset($_POST['acf']['field_60526244663f2'])) : // replace with your field key
                 $_POST['acf']['field_60526244663f2'] = $new_calc_collected_from_other_sources;
               else :  update_field('calc_collected_to_date_not_s4f',$new_calc_collected_from_other_sources,$post_id);
            if (isset($_POST['acf']['field_6052632c663f5'])) : // replace with your field key
                 $_POST['acf']['field_6052632c663f5'] = $new_calc_total_collected;
               else :  update_field('calc_grand_total',$new_calc_total_collected,$post_id);
        } // end post type
    }// end function s4f_save_post_step_content
  • Anybody have any ideas or where I can look

  • If you want to modify values as shown here

    $_POST['acf']['field_60526244663f2'] = $new_calc_collected_from_other_sources;

    so that acf uses these values when it updates fields then you need to use a priority in your add_action() of < 10 so that it happens before ACF saves values.

    add_action('acf/save_post', 's4f_save_post_step_content', 1);

    Once ACF has updated values at priority 10, altering the contents of $_POST['acf'] will have no effect.

  • Actually what is happening is I am creating a new value to save that I think gets saved but then the $_POST['acf'] writes over it with the Post value.

    So I think I need way to save the new data and save/update the values after the $_POST['acf']

    Does that make more sense.

    I was hoping to do this here as I have all the data that I need here.

  • From your code above

    add_action('acf/save_post', 's4f_save_post_step_content', 20);

    At priority 20 ACF is done. ACF runs at priority 10. This means that anything you do after will not be overwritten by ACF.

    $_POST['acf']['field_6052632c663f5'] = $new_calc_total_collected;

    This will have no effect whatsoever. As I said above. ACF has finished looking at these values on priority 10 so changing these values will not effect what ACF has already done with them. You are changing them at a point where the values no longer matter. Processing of this array has already been completed.

    if you add an action with a priority < 10 like this

    add_action('acf/save_post', 's4f_save_post_step_content', 1);

    This will happen before ACF looks at what is located in $_POST['acf']. Making changes to this array at this point will cause ACF to save the changes you make.

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

You must be logged in to reply to this topic.