Support

Account

Home Forums General Issues Reset repeater data / or overwrite all repeater data before updating post

Unread

Reset repeater data / or overwrite all repeater data before updating post

  • Hey Guys,

    I am trying the following thing: In the options i there are mulitple repeaters filled with data. Within a post i populate a repeater with data from a repeater set in the options (this is working fine). With a select field you can choose from wich repeater field from the options, the repeater in the post should be populated with.(also this is working).

    The problem is when changing the select field and saving the post, the repeater data has to be reset before loading the new data from a options page repeater. Because when the post is loading data from a repeater field with 5 rows, changing to loading data from a repreater field with just 3 rows, leaves 2 too much rows in the post repeater. I do not insert the same amount of rows again. So the repeater in the post must match the repeater in the options.

    I don’t really understand the update_field method, is it completely overwriting the repeater data or just updating the repeater data? This topic(https://support.advancedcustomfields.com/forums/topic/delete-all-repeater-rows-2/) has almost the same question and the answer of JAMES is telling me that the update function will completely rewrite the repeater data. So i have written this function but its not working:

    
    add_filter('acf/update_value/name=product_specs_set', 'check_for_specset_change', 5, 3);
    
    function check_for_specset_change($value, $post_id, $field) {
      $old_value = get_post_meta($post_id, 'product_specs_set', true);
      // $productSpecsRepeater = get_field('product_specs', $post_id);
      if ($old_value != $value) {
        $testSets = get_field('spec_sets', 'option');
        $selectedId = get_field('product_specs_set', $post_id);
        $attributes = array();
        foreach ($testSets as $key => $testSet) {
          if($selectedId == $testSet['spec_set_id']){
             foreach ($testSet['spec_atributes'] as $key => $spec) {
                $attributes[] = array(
                  "field_5ceb9f6ff8c19" => $spec['spec_title'], 
                  "field_5ceb9f85f8c1a" => "empty"
                );
             }
          }
        }
        // it is coming to this point and de $attributes array is looking good. But the field is not updating at all.
        update_field('field_5ceb9f15f8c18', $attributes, $post_id);
        }   
    
      return $value;
    }
    

    And i have tried deleting all rows before saving the data but it is not deleting the rows:

    
    function my_acf_save_post( $post_id ) {
    
     // if($post->post_type == 'producten'){
    
        $old_value = get_post_meta($post_id, 'product_specs_set', true);
        $value = $_POST['acf']['field_5ceba0f9f8c1c'];
    
        if ($old_value != $value) {
          $repeaterValues = $_POST['acf']['field_5ceb9f15f8c18'];
          // --------  even this is not working, why?
          delete_row('field_5ceb9f15f8c18', 1, $post_id);
          delete_row('field_5ceb9f15f8c18', 2, $post_id);
          delete_row('field_5ceb9f15f8c18', 3, $post_id);
          // -----------
          $i = 1;
          
          foreach ($repeaterValues as $key => $value) {
             delete_row('field_5ceb9f15f8c18', $i, $post_id);
             $i++;
          }
          // for ($i=1; $i <= count($repeaterValues); $i++) { 
          //    delete_row('field_5ceb9f15f8c18', $i, $post_id);
          // }
        }
     // }
     
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 1);
    

    Thanks for your help!

Viewing 1 post (of 1 total)

The topic ‘Reset repeater data / or overwrite all repeater data before updating post’ is closed to new replies.