0

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 ^^ !

  • Any idea to answer my question guys ? I'm stuck on this for a week and didn't find an answer – jeannot May 21 at 5:44
  • So anybody know ? i doesn't move forward beacause i'm unable to understand where my problem is – jeannot May 28 at 6:21
  • Did you get this figured out? – MikeNGarrett Jun 13 at 13:36
  • Hi ! Nop, no answer from anyone, if you have any ideas i take it pleasurely – jeannot Jun 14 at 6:08
  • Ok, I ran through your code and I'm very confused. This code filters the value of these fields after they're already selected. If you don't return the single ID or term object, this will fail. I don't know what you're trying to accomplish exactly, but I'm fairly certain this isn't the way to do it. Could you provide some details as to how this is supposed to work? – MikeNGarrett Jun 14 at 15:09
0

Thanks for posting that additional information. I think I understand what you're trying to accomplish now. Instead of filtering the value like you are now, all you have to do is update the field value during the migration.

update_field($selector, $value, $post_id); Learn more about updating ACF field data.

  • i don't get it i'm sorry, you mean remove the 'add_filter('acf/load_value/name=villes_a_ajouter_III', 'acf_load_my_load_cat', 10, 3);' to replace it with 'update_field( $field_key, $value, $post_id );'? I tried but this is not working.Or do i have to import, run the code then remove it ? if this is it i cannot do that, i'm using wp all import and i don't know where do i have to put this. There is function editor in it, but doesn't seem to do this – jeannot Jun 18 at 9:23
  • I'm not familiar with wp all import, but the filter you're using for acf/load_value doesn't actually save the field's value to the database. update_field does, but it doesn't hook into the import process since it's not a filter or action. You'll have to figure out what action to use to make this work correctly. – MikeNGarrett Jun 18 at 15:23
  • Ok, i'm gonna try something (sorry for the time to answer), and i keep you in touch as long as i move forward on this ! – jeannot Jun 21 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.