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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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.