i have a repeater field called books and a subfield call booklink. i am trying to add row to booklink by data submitted via frontend form. but it never adds the data
$booklink=$_POST[‘booklink’];
update_field(‘booklink’,$booklink,$post_id);
Hi @littledevil
You need to get the repeater first before updating it. I think something like this:
$booklink = $_POST['booklink'];
$field_key = "books";
$value = get_field($field_key, $post_id);
$value[] = array("booklink" => $booklink );
update_field( $field_key, $value, $post_id );
I hope this helps.
how do i delete a row from subfield of repeater ?
Hi @littledevil
Do you have some rules to select the row you want to delete? Maybe the first row or a row with a certain value?
i have id and image link in each row of the subfield…i want to delete the row if1st the id matches and then the image link matches to the one entered.
Hi @littledevil
First, you can use this code to show the content of your repeater:
<pre>
<?php
print_r(get_field('books'));
?>
</pre>
Then, you can loop the returned data and check if the id or the link match or not. This comment has a good example to help you start with it: http://php.net/manual/en/function.array-search.php#91365.
Hope this helps.
no i am able to select the row based on the criteria…but what command do i use to delete the row.
Hi @littledevil
You can use the unset() function to delete the row from the returned repeater data. After that, you can use the array_values() to fix the key index. This answer should give you more idea about it: http://stackoverflow.com/questions/369602/delete-an-element-from-an-array#answer-369761.
So, when you update the repeater using this code: update_field( $field_key, $value, $post_id );
, it will use the new data without the deleted rows.
I hope this helps.