Support

Account

Home Forums Add-ons Repeater Field Taxonomy field inside a repeater not saving properly?

Solving

Taxonomy field inside a repeater not saving properly?

  • Hi there — I’m having a problem with a taxonomy field inside a repeater. The idea is that each row is a link to a foreign-language translation of one of our articles — with three fields, the translating publisher, the URL of the translation, and the language of the translation. The first two are text fields and work fine.

    The taxonomy, however, is screwy. The taxonomy currently has three terms in it (Spanish, Portuguese, and Russian) and it lets me select those. But when I save the post, all three change to Russian. That’s despite the fact that the metabox for that taxonomy (visible elsewhere on the edit page) shows all three terms as being attached to the post.

    Can you think of any reason why it might be switching everything to Russian on every save? (I mean besides Putin-paid hackers. 🙂 )

  • This is probably because you have selected “Load Term” and “Save Terms”. This causes ACF to update the terms separately for each field and the only one that sticks is the last one. Basically, you can’t load and save terms when dealing with a repeater.

    I’ve seen this discussed before, but I don’t know if it was ever solved by anyone.

    I would probably turn off these settings for the taxonomy field and I would create my own acf/update_value for the repeater field that would set the post terms so that multiple terms can be added all at once https://codex.wordpress.org/Function_Reference/wp_set_post_terms

  • Thanks so much for the reply, John. But…this seems like a pretty big bug, no? You just can’t reliably save any values in a repeater to a taxonomy?

    I confess I don’t really have any idea how to write the sort of code you recommend. Is there any place you could point me to that might clear things up? Thanks again.

  • It’s not really a bug, the taxonomy field in one row of a repeater does not know what’s going on with the same field in another row. It would be possible to “append” values, but then there is the issue with “removing” values first. For example, in you changed the selected term in a row and ACF used the append terms version of the function then the term you wanted to remove would still be there. ACF solves this by replacing all the terms whenever it updates a taxonomy field.

    At any rate, I would to something along the lines of this after change the field to not load or set terms. But you should be aware that you will not be able to set these taxonomy/terms anywhere but in the ACF field.

    
    add_action('acf/save_post', 'update_repeater_post_terms');
    function update_repeater_post_terms($post_id) {
      if (get_post_type($post_id) != 'YOUR POST TYPE HERE') {
        return;
      }
      $terms = NULL; // this will clear terms if none found
      if (have_rows('YOUR REPEATER', $post_id)) {
        $terms = array();
        while (have_rows('YOUR REPEATER', $post_id)) {
          // add this term to the array
          $terms[] = get_sub_field('YOUR SUB FIELD', false); // false for no formatting
        }
      }
      set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false);
    }
    
  • This really needs some kind of warning in the admin interface I feel to make it more user friendly! Its a very easy confusion to make and nothing suggests this could happen and not work as I believe 99% of people will imagine it would.

  • John,
    Thanks for the clarification. I wish there was a warning too because it took me hours of trial and error. I thought I was doing something wrong, until I arrived here.

    I think the last line before closing your function should be
    wp_set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false);

    The other one you have (set_oject_terms) is the hook… but I’m not an expert on this.

    Thanks so much!

  • This was very useful and pointed me in the right direction. I’m not an expert by any means, so forgive me If what I’m saying is mistaken. The code seems to have to small errors, that can go silently unreported and prevent admin pages from saving properly, at least that was my experience and it took me forever to realize what is going on.

    Below I’m pasting the amended code, with comments on what was missing or needs to be changed in case someone else, like me, needs to use this. Thanks so much anyways because this made a difference.

    
    add_action('acf/save_post', 'update_repeater_post_terms');
    function update_repeater_post_terms($post_id) {
      if (get_post_type($post_id) != 'YOUR POST TYPE HERE') {
        return;
      }
      $terms = NULL; // this will clear terms if none found
      if (have_rows('YOUR REPEATER', $post_id)) {
        $terms = array();
        while (have_rows('YOUR REPEATER', $post_id)) {
    
          // SET THE ROW, WAS MISSING      
          the_row();
          // add this term to the array
          $terms[] = get_sub_field('YOUR SUB FIELD', false); // false for no formatting
        }
      }
    
      // DO NOT THINK THIS IS THE RIGHT WAY, set_objects_terms is a hook 
      // set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false);
      // INSTEAD WE NEED THIS SIMILARLY NAMED FUNCTION:
      wp_set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false); 
    
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.