Support

Account

Home Forums ACF PRO How do repeater and relationships work together

Solving

How do repeater and relationships work together

  • Hi. I really like the ACF and has just bought the pro.

    I’m trying to get the repeater and relationships to work together.
    But do not get it.

    The page I use to test me out on:
    http://driftmagazine.se/blog/resultat/resultat-drift-sm-r1-2015-grondal/

    Repeater:
    resultat_2015_r1

    Relationship:
    tavlingsforare_post

    Sub: – this is ok 🙂 but I don’t anderstand how to make the design or

  • resultat_r1_poang

    
    
    <?php
    // check if the repeater field has rows of data
    if( have_rows('resultat_2015_r1') ):
    
     	// loop through the rows of data
        while ( have_rows('resultat_2015_r1') ) : the_row();
    
            // display a sub field value
            the_sub_field('resultat_r1_poang');
    		
    		the_sub_field('resultat_r1_placering');
    		
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    ?>
    
  • That looks right so far, you just need to build a nested loop inside your while loop to display the posts from the relationship field http://www.advancedcustomfields.com/resources/relationship/

  • Thanks. I have tested all the examples on the page and also a relationship that I have on another page without repeaters. But I get no work. This is how my last attempt looks.

    
    <?php if( have_rows('resultat_2015_r1') ): ?>
    
    	<ul class="slides">
        
    	<?php while( have_rows('resultat_2015_r1') ): the_row(); 
    
    		// vars
    		$image = get_sub_field('image');
    		$content = get_sub_field('content');
    		$link = get_sub_field('link');
    	
    		
    		
    		the_sub_field('resultat_r1_placering');
    		
    		
    		the_sub_field('resultat_r1_poang');
    		
    	
    
    						/*
    						*  Query posts for a relationship value.
    						*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    						*/
    
    						$forare = get_posts(array(
    							'post_type' => 'forare',
    							'meta_query' => array(
    								array(
    									'key' => 'tavlingsforare_post', // name of custom field
    									'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE'
    								)
    							)
    						));
    
    						?>
    						<?php if( $forare ): ?>
    							<ul>
    							<?php foreach( $forare as $forare ): ?>
    								
    								<li>
    									<a href="<?php echo get_permalink( $forare->ID ); ?>">
    										
    										<?php echo get_the_title( $forare->ID ); ?>
    									</a>
    								</li>
    							<?php endforeach; ?>
    							</ul>
    						<?php endif; 
    
    		?>
    
            
            
    		<li class="slide">
            
            
    
    			<?php if( $link ): ?>
    				<a href="<?php echo $link; ?>">
    			<?php endif; ?>
    
    				<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
                    
                    
    
    			<?php if( $link ): ?> 
    				</a>
    			<?php endif; ?>
                
                	
    
    		    <?php echo $content; ?> innehåll - tavlingsforare_post
                
            
                
    
    		</li>
    
    	<?php endwhile; ?>
    
    	</ul>
    
    <?php endif; ?>
    
  • I’m not sure what you’re doing here, but you said that one of the fields is a relationship field, a relationship field returns the list of posts that are selected. If you’re getting a relationship field why are you doing a query to get posts?

  • Sorry, do not have a clue.
    I’m not so good at this so deep encoding so I have more or less just cut in and tested all possible variants without really understanding what I do.

    I would like to do the following

    ACF

    It is a result list.

    First I want to retrieve a user if there is one.
    If not, I want to retrieve a relationship that is a custom post type named forare (drivers).

    Then I want to get the results that are sub fields and that’s just the ones I’ve got it right. 🙂

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

    You must be logged in to reply to this topic.