Support

Account

Home Forums Add-ons Repeater Field Delete a repeater field via script Reply To: Delete a repeater field via script

  • After struggling with this for quite some time, I was able to find a helpful solution on Stack Exchange here http://stackoverflow.com/questions/24879399/wordpress-acf-how-to-add-rows-to-a-repeater-field-attached-to-a-user-via-custom

    I thought I’d go ahead and post the answer here since I’m sure I’m not the only one who has ran into this same issue of trying to add or remove repeater rows.

    I know it may come across as dumbing it down too much, or over explaining it, but hopefully it will help anyone who runs into this same issue. I’m very surprised there isn’t more out there about this, since it seems to be a pretty common thing.

    Here’s the code and then a breakdown of what each piece is.

    $post_ID = 'id_of_post_youre_updating';
    $field_key = 'field_key_youre_updating';
    $value = get_field( $field_key, $post_ID );
    $value = removeElementWithValue($value, 'sub_field', 'find_this_value');
    
    update_field( $field_key, $value, $post_ID);

    $field_key is the ‘field key’ associated to the repeater field you are trying to update.
    If you need help finding the field key, reference ‘Finding the field key’ on this page http://www.advancedcustomfields.com/resources/update_field/

    $value is retrieving the field (based on the $field_key) associated to the ID ($post_ID) of the post you’re trying to update. This value will be stored in an array.

    The next part is where all of the magic happens. Since the repeater field’s information is stored in a multidimensional array, we need to loop through that array and strip out the value you’re wanting to remove. That’s where the removeElementWithValue() function comes in — I’ll explain that in a moment.

    The ‘sub_field’ will be the subfield of the repeater field where the value is stored, and ‘find_this_value’ is the value you’re looking for, to determine which row to remove.

    The ‘update_field’ string, is what will update the repeater field with the new array (created by the removeElementWithValue function)

    Here is the code for the removeElementWithValue function. This function will loop through a multidimensional array, and strip out the value you’ve supplied. This function can be simply included in your functions.php file, without any alteration.

    function removeElementWithValue($array, $key, $value){
         foreach($array as $subKey => $subArray){
              if($subArray[$key] == $value){
                   unset($array[$subKey]);
              }
         }
         return $array;
    }

    As an added bonus, if you would like to ADD a row to a repeater field, all it takes is a small change to the first block of code. Rather than calling the removeElementWithValue function, you’ll change that line of code to…

    $value[] = array('sub_field' => 'value_you_wish_to_add');

    where the ‘value_you_wish_to_add’ is the value you want added to the new row. You can add multiple rows by adding multiple lines.

    $value[] = array('sub_field' => 'value_you_wish_to_add');
    $value[] = array('sub_field' => 'value_you_wish_to_add_2');
    $value[] = array('sub_field' => 'value_you_wish_to_add_3');

    So the final block of code to ADD a row would be

    $post_ID = 'id_of_post_youre_updating';
    $field_key = 'field_key_youre_updating';
    $value = get_field( $field_key, $post_ID );
    $value[] = array('sub_field' => 'value_you_wish_to_add');
    
    update_field( $field_key, $value, $post_ID);

    Hope this helps.