Search code examples
wordpressadvanced-custom-fieldsacfpro

ACF repeater in taxonomy (category) doesn't work


In a taxonomy category I have:

Repeater field named questions. Inside a repeater I have a field named answer.

I am trying to show all answers in category.php file like this:

$term = get_queried_object();

if ( have_rows( 'questions',   $term ) ) {
    while( have_rows( 'questions',   $term ) ) {
        the_row();
        the_sub_field( 'answer' );
    }
}

It doesn't work. Can you please tell me what is wrong here? I have tried for hours to make it work.


Solution

  • I finally figured it out. I checked in phpmyadmin where the data is stored. It turned out that the data is saved in wp_termmeta and not as I thought in wp_postmeta. This is why most of the solutions didn't work.

    Working workaround code for repeater added to taxonomy (a category in my example) using get_term_meta instead of ACF code (loops and functions).

    <?php
      // name of repeater field
      $repeater = 'questions'; 
    
      // get taxonomy id
      $taxonomy_id = get_queried_object_id(); 
    
      // get repeater data from term meta
      $post_meta = get_term_meta($taxonomy_id, $repeater, true);
    
      // count items in repeater
      $count = intval(get_term_meta($taxonomy_id, $repeater, true));
    
      // loop + apply filter the_content to preserve html formatting
      for ($i=0; $i<$count; $i++) {
          echo apply_filters('the_content', get_term_meta($taxonomy_id, $repeater.'_'.$i.'_'.'title', true));
          echo apply_filters('the_content', get_term_meta($taxonomy_id, $repeater.'_'.$i.'_'.'answer', true));
        }
      ?>
    

    The solution from documentation still doesn't work for repeaters in taxonomy. It does work for non-repeaters (ex. image, text added to taxonomy). https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/