Home › Forums › Add-ons › Repeater Field › Dynamically add rows to repeater field on post load › Reply To: Dynamically add rows to repeater field on post load
You’re trying to use acf/load_field filter and you should probably be using the acf/load_value filter, something like this
function acf_load_taxonomy_custom_urls_table($value, $post_id, $field){
$categories = get_the_terms($post_id, 'category');
foreach ($categories as $category){
echo("<script>console.log('PHP: " . $category->term_id . "');</script>");
$value[] = array(
'field_5b1d513f4bc65' => $category->name,
'field_5b1d51a24bc66' => '',
'field_5b1d51e74bc67' => '',
);
}
return $value;
}
add_filter('acf/load_value/key=field_5b1d50cf4bc64', 'acf_load_taxonomy_custom_urls_table', 20, 3);
Your main problem here is going to be that your categories will be added every time you save and the post refreshes, adding the same values over and over again. What you’ll need to do it to loop over the existing value and make sure each value does not already exist before adding a new row. And then there is the fact that any values deleted will not be deleted unless you also test to see if any categories have been removed and removes those rows from the repeater array.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.