Support

Account

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

Solving

Delete a repeater field via script

  • Hello,

    I have a website where users can add events to a calendar and I do this by making the events part of a repeater field.

    I have worked out a script for them to add an event as a repeater (update_field with an array), but I cannot find the code for when they wish to delete an event (i.e. remove a particular repeater field row).

    Is this possible from outside the back end?

    Thanks

    p.s. love the plugin

  • Hi @matthewpont

    You will need to again use the update_field function, but first remove the row from the $value.

    Hope that helps.

    Thanks
    E

  • I got this working so thought I’d share code:

    // instance vars.
    		$uid        = $_POST['user_id'];       // current user ID
    		$field_key  = "field_5344787df3a9b";   // ACF field name
    		$post_id    = $_POST['post_id'];       // Post ID of draw to update
    		
    		// Get member_entries field
    		$value = get_field($field_key, $post_id);
    
    		$new_value = array();
    		foreach ( $value as $id => $entry ) {
    			if ($entry['user_id'] == $uid) continue;
    			$new_value[$id] = $entry;
    		}
    		
    		$status = update_field( $field_key, $new_value, $post_id );
  • Very helpful for a completely different reason, thanks!

  • Hey – could anyone elaborate on how the row is actually ‘deleted’ – do you just set the $new_value to empty?

  • 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.

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

The topic ‘Delete a repeater field via script’ is closed to new replies.