Home › Forums › Add-ons › Repeater Field › Pre-fill repeater field
I’m in trouble right now and I need you to help on a little problem that I have.
For one of my clients I need to use ACF repeater field and populate it with empty categories from a custom taxonomy
So I started with something like this:
`function acf_load_my_load_cat($value, $post_id, $field){
$args = array( ‘taxonomy’ => ‘ville_formation’);
$post_categories = wp_get_post_terms( get_the_ID(), ‘ville_formation’, array(“fields” => “ids”) );
if ( !empty( $post_categories ) ) {
$args[‘exclude’] = $post_categories;
};
$categories = get_terms( $args );`
So basically created a function that grab my taxonomy (wich is called ville_formation
) and defined which term is not empty, excluded it and stored the others on a variable named $categories
` $value=array();
foreach ( $categories as $category ) {
$value [] = array(
‘field_5cd186302ef1c’ => $category->name);
};`
Now with this I’m creating an empty array, and for each stored term, I create a row on my array and I assign it a key that is the key of the field (here : ‘field_5cd186302ef1c’)
`return $value;
};
add_filter(‘acf/load_value/name=villes_a_ajouter_III’, ‘acf_load_my_load_cat’, 10, 3);`
And at last I return the values and load my array on my repeater field (here called : ‘villes_a_ajouter_III’)
This seem to work correctly, but in this repeater that I’m populating with this function, I have some radio buttons next to it, and every time that I made a choice and update my post, the choice on this radio field is not saved.
My problem is that my code is creating an empty array every time in order to populate my field. How do I proceed to avoid this, in order to save my choices from this group of radio button?
So any idea to add/modify something to store the value from those radio buttons?
PS : For those who want the entire code :
`function acf_load_my_load_cat($value, $post_id, $field){
$args = array( ‘taxonomy’ => ‘ville_formation’);
$post_categories = wp_get_post_terms( get_the_ID(), ‘ville_formation’, array(“fields” => “ids”) );
if ( !empty( $post_categories ) ) {
$args[‘exclude’] = $post_categories;
};
$categories = get_terms( $args );
$value=array();
foreach ( $categories as $category ) {
$value [] = array(
‘field_5cd186302ef1c’ => $category->name);
};
return $value;
};
add_filter(‘acf/load_value/name=villes_a_ajouter_III’, ‘acf_load_my_load_cat’, 10, 3);`
no help for me on this problem ? i’m stuck on this for weeks, if you have any helps i take it !
You must be logged in to reply to this topic.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 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.