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);
---EDIT 17/06---
Okey so i'm gonna explain a little bit more.For one of my client i have to import some data to create some custom post, with some dates/cities inside. So in Advanced Custom Field i've created a repeater field, and with a plugin i've created a custom taxonomy named "Villes_formation". So the import is going well, the custom taxonomy inside "villes_formation" are created with the import and show inside a custom post type that i've created for my needs.
To represent it it show me something like that https://zupimages.net/up/19/25/ji2y.png
On my pic, the cities are on the left (On the column named "Villes"), and as you can see on the right there is some radio buttons with some choices. The column named "Dates" associate the dates with cities. This one is going well, and work as i want.
But, now my client want to have a another repeater field with only the cities that belong to my taxonomy " Villes_formation" and that are not assigned to the post.(there is some that are not created with the import) Let's assume that in my custom taxonomy "Villes_formation" i have those lines:
-Nantes(44) -Rennes(35) -Paris(75) -Niort(79) -Nice(06) -Lyon(69) -Poitiers(86) -Quimper(29)
The function that i've created is showing only the cities of this list that are not assigned to the current post by the import.
To be more clear here is a pic of the second results in my post https://zupimages.net/up/19/25/sxm0.png
As you can see with my list and the two pics, there is 2 repeater field, one with the cities assigned to the post, one with the cities that are not.
This function indeed show me the "Villes_formation" that are not assigned to this post, but on the second pic you can also see on the right three radio buttons that are here to make a choice on every city.
So when i make a choice on this one it is not saved, every time that i click on "update", my function is creating a new array with cities that are not assigned to the post, and erase my previous choices.
My goal is to saved those choices of course !
Do i have to create something that check the radio buttons/cities before to erase them ?
I hope i have been clearer ^^ !