Home › Forums › General Issues › Add repeater row on post save, only if other field is updated › Reply To: Add repeater row on post save, only if other field is updated
In the scenario what you have outlined, that is to add a row to a repeater only if some other field has changed. This depends on if the repeater is being submitted when the other field is submitted.
Your comment
If I use the acf/save_post action with a priority < 10 (before the post is saved), I can compare the field’s value in $_POST compared to what’s currently in the DB for the post via get_field(). However, if I try to use update_field() to add the repeater row, it gets overwritten when $_POST is actually saved — so the new row gets cleared out.
tells me that this is the case.
For this I would use an acf/save_post filter with a priority of < 10 so that I can compare the new/old values. In this filter I would modify $_POST[‘acf’] with the added row so that ACF can then update the repeater.
// example contents of acf/save_post action
// check to see if the value is change
if ($_POST['acf']['field_XYZ123'] != get_field('field_name', $post_id)) {
// build new repeater row
$row = array(
// the row requires sub field key => value pairs
'field_123ACB' => 'new sub field value for this row';
);
// add this row to the submitted repeater
$_POST['acf']['field_ZYX321'][] = $row;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.