Support

Account

Home Forums Add-ons Repeater Field Nested Repeaters Using Get Post Meta

Solved

Nested Repeaters Using Get Post Meta

    • Paul

    • September 4, 2019 at 9:48 pm

    Hey,

    I’m currently building a theme using ACF. I’m using get_post_meta throughout rather than using get_field and I’ve got all working, however, I’ve run into an issue using a nested repeater, I can seem to get the nested fields to display.

    Here’s the code I’m working with:

    <?php if ( $badges ) {
    
    	for ( $i = 0; $i < $badges; $i++ ) {
    		$badges_title       = get_post_meta( $post->ID, 'badges_' . $i . '_badges_title', true );
    		$badges_description = get_post_meta( $post->ID, 'badges_' . $i . '_badges_description', true );
    	?>
    
    	<!-- This repeater is displaying no problem -->
    	<?php if ( $badges_title && $badges_description ) { ?>
    		<h3><?php echo wp_kses_post( $badges_title ); ?></h3>
    		<p><?php echo wp_kses_post( $badges_description ); ?></p>
    	<?php } ?>
    
    		<!-- This nested repeater is not displaying -->
    		<?php if ( $badge ) {
    
    			for ( $i = 0; $i < $badge; $i++ ) {
    
    				$badge_title       = get_post_meta( $post->ID, 'badge_' . $i . '_badge_title', true );
    				$badge_description = get_post_meta( $post->ID, 'badge_' . $i . '_badge_description', true );
    			?>
    
    			<?php if ( $badge_title && $badge_description ) { ?>
    				<h3><?php echo wp_kses_post( $badge_title ); ?></h3>
    				<p><?php echo wp_kses_post( $badge_description ); ?></p>
    			<?php } ?>
    
    			<?php }
    
    		} ?>
    
    	<?php }
    
    } ?>

    Any ideas?

    Thanks in advanced.
    Paul

  • I can’t tell by looking at your code what you’re trying to do, I don’t see any “nesting”. Here is basic code to get sub fields of a nested repeater.

    
    $repeater = 'repeater_name';
    $repeater_count = intval(get_post_meta($post_id, $repeater, true));
    $nested_repeater = 'nested_repeater_name';
    for ($i=0; $i<$repeater_count; $i++) {
      $nested_count = intval(get_post_meta($post_id, $repeater.'_'.$i.'_'.$nested_repeater, true);
      for ($j=0; $j<$nested_count; $j++) {
        // get value of field in nested repeater
        $value = get_post_meta($post_id, $repeater.'_'.$i.'_'.$nested_repeater.'_'.$j.'_sub_field_name', true);
      }
    }
    
Viewing 2 posts - 1 through 2 (of 2 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.