Support

Account

Home Forums Add-ons Repeater Field Get custom field values from relationship post object inside repeater

Solved

Get custom field values from relationship post object inside repeater

  • I have a repeater field which contains a relationship subfield (named “api_references”). The relationship post object is calling a custom post type “references” to display. When you open the example that I have so far, you’ll see 4 sections with the button “Modal!” When you click that button, the relationship posts are intended to be displayed along with their custom field values.

    The problem is that each button is supposed to return a different set of results, but they are all returning the same results (which only belong to the first).

    I think it may have something to do with the relationship fields count, but I have not had any luck finding the correct code to resolve it. Any ideas? It would be much appreciated.

    <?php 
    	$posts = get_sub_field('api_references');
    	if( $posts ): ?>
    
    	<div class="modal">
    		<label class="modal__bg" for="modal-1"></label>
    		<div class="modal__inner">
        			<label class="modal__close" for="modal-1"></label>
    			<h2>References</h2>
    			<ul>
    				<li class="header"><span>Title</span><span>Description</span><span class="number">Ref #</span><span>PDF</span></li>
    			</ul>
    
    			<ul>
    			<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    			<?php setup_postdata($post); ?>
    				<li>
    					<span><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    					<span><?php the_field('reference_description'); ?></span>
    					<span><?php the_field('reference_number'); ?></span>
    					<span><?php the_field('reference_pdf'); ?></span>
    				</li>
    			<?php endforeach; ?>
    			</ul>
    		</div><!-- end modal-inner -->
    	</div><!-- end modal -->
    	<?php wp_reset_postdata(); ?>
    <?php endif; ?>
  • There is nothing in the code you have posted that would cause a problem. I don’t see any have_rows() repeater loop in your code or an indication of how you’re getting each row of the repeater. That is probably were you’re problem is.

  • I actually had the repeater loop code but just didn’t post it. I was able to solve it though! I needed a PHP increment bit of code to get it working. Here is the final code in case it helps anyone else:

    <?php if( have_rows('api_properties') ): ?>
       <?php $id_increment = 0; ?>
    
    	<?php while ( have_rows('api_properties') ) : the_row(); ?>
    
    		<li>
    
    		<?php 
    			$name = get_sub_field('api_name'); 
    			$value = get_sub_field('api_value');
    			$posts = get_sub_field('api_references');
    		?>
    
    		<?php if( $posts ): ?>
    
    			<h3 class="api-category"><!-- Button trigger modal -->
    			<button onclick="document.getElementById('id1_<?php echo $id_increment; ?>').style.display='block'" class="w3-button"></button><?php echo $name; ?></h3>
    			<p class="api-name"><?php echo $value; ?></p>
    
    			<div id="id1_<?php echo $id_increment; ?>" class="w3-modal">
    				<div class="w3-modal-content">
    				  <header class="w3-container w3-teal"> 
    					<span onclick="document.getElementById('id1_<?php echo $id_increment; ?>').style.display='none'" 
    					class="w3-button w3-display-topright">&times;</span>
    					<h3>References</h3>
    					</header>
    
    				<div class="w3-container results">
    				<table width="100%" cellpadding="0" cellspacing="0" border="0">
    					<tr>
    						<td class="header">Title</td>
    						<td class="header">Description</td>
    						<td class="header number">Ref #</td>
    						<td class="header">PDF</td>
    					</tr>      
    
    					<?php foreach( $posts as $p ): ?>
    						<tr>
    							<td><?php echo get_the_title( $p->ID ); ?></td>
    							<td><?php echo get_field('reference_description', $p->ID); ?></td>
    							<td class="number"><?php echo get_field('reference_number', $p->ID); ?></td>
    							<td><?php echo get_field('reference_pdf', $p->ID); ?></td>
    						</tr>
    					<?php endforeach; ?>
    
    				  </table>
    			  </div>
    			  <footer class="w3-container">
    				<p>&nbsp;</p>
    			  </footer>
    		  </div>
    
    		 <?php $id_increment++; ?>
    
    		</div>
    
    		<?php else : ?>
    
    		<h2 class="api-category"><?php echo $name; ?></h2>
    		<p class="api-name"><?php echo $value; ?></p>
    
    		<?php endif; ?>
    
    	</li>
    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
  • anyway to do this with the wp api? thanks!

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

The topic ‘Get custom field values from relationship post object inside repeater’ is closed to new replies.