1

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.

Tomasz Muter
  • 141
  • 1
  • 2
  • 7

3 Answers3

4

If anyone's still looking for a solution, heres how I did it.

            <?php 
                $term = get_queried_object();
                $taxonomy = $term->taxonomy;
                $term_id = $term->term_id;  
                if( have_rows('highlight_boxes', $taxonomy . '_' . $term_id) ):
                while(have_rows('highlight_boxes', $taxonomy . '_' . $term_id)): 
                the_row(); 
            ?> 

                <div class="column">
                    <h2 class="heading-title"><?php the_sub_field('highlight_title', $taxonomy . '_' . $term_id) ?></h2>
                    <div class="description">
                        <p><?php the_sub_field('highlight_desc', $taxonomy . '_' . $term_id) ?></p>
                    </div>
                </div>
            
            <?php endwhile; endif; ?>  
sJirel
  • 41
  • 4
2

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/

Tomasz Muter
  • 141
  • 1
  • 2
  • 7
  • Yes, I agree with you. The official documentation still doesn't work for repeaters in taxonomy after exactly 2 years. I have used the manual approach. I checked in PHPMyAdmin where the data is stored and fetched the data accordingly. – Bhavin Patel Aug 04 '22 at 10:36
1

Have you tried looping through your field as an array instead? Often that's easier, especially if you are getting fields that aren't part of the global $post. Instead your setup would look like:

$questions = get_field( 'questions', $term );
foreach( $questions as $question ){
    echo $question[ 'answer' ];
}

I find that much easier. I would guess your issue above has to do with the_row() not getting the global loop properly because you're accessing the fields of $term but I'm not sure. In any case - the code above should work.

JBoss
  • 696
  • 5
  • 17
  • I am sad to say, but it still doesn't work. I tried with for and foreach loops. Still can't figure it out why it doesn't work. – Tomasz Muter Aug 05 '20 at 20:10
  • Okay, so what happens if in the above code - you dump out $questions? And for that matter, '$term'. Is it the correct term you're expecting? And if so - does $questions contain anything? – JBoss Aug 05 '20 at 20:21
  • When I var_dump $questions it shows `string(1) "5"` I have 5 elements added to repeater. Showing string instead of ex. array is also mentioned here https://support.advancedcustomfields.com/forums/topic/repeater-field-returns-string/ – Tomasz Muter Aug 05 '20 at 20:36
  • hmm okay yeah that's really strange. Looks like that forum topic is your best bet then. You must have something else interfering. – JBoss Aug 05 '20 at 20:41
  • I added working solution based on get_term_meta. Thank you for trying to help me. – Tomasz Muter Aug 06 '20 at 10:02
  • 1
    Yeah sounds like that might be the only way if this is a compatibility problem. The only thing to take note of there: you're now pulling the value straight from the database - so this does NOT pass through any ACF filters now. So if you have formatting applied in ACF (for example - the return format of the data), that won't be applied. But may be your best bet even so – JBoss Aug 06 '20 at 17:50
  • JBoss you are right. I noticed it after a while that paragraphs are not added. I've updated it with the_content filter. Thanks again! – Tomasz Muter Aug 07 '20 at 14:43