Support

Account

Home Forums Add-ons Repeater Field Relationship in repeater field

Solved

Relationship in repeater field

  • Hello guys !

    I’m following this tutorial and I try to apply it to my website.
    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    I have artists thats plays in places.
    An artist can play in different places, so i want to compare the row of the repeater field with the current place ID. And only show the row where the place is the same that the page.

    But i don’t know how to achieve it…

    Anyone can help me ?

    Thanks

    Here is the code for the place : single-place.php

    
    	<?php 
    	function allow_wildcards( $where ) {global $wpdb;
    	$where = str_replace("meta_key = "representation_%", "meta_key LIKE }representation_%",
    	$wpdb->remove_placeholder_escape($where)
    	);return $where;}
    	add_filter('posts_where', 'allow_wildcards');
    	?>	
    		
    	<?php 
    	$query = new WP_Query(array('post_type' => 'artiste',
        'meta_query' => array(
        	array(
        		'meta_key'	=> 'representation_%_lieu_representation',
        		'value' => '"' . get_the_ID() . '"', 
        		'compare' => 'LIKE',
        		)
        	)
        ));
    	?>
    
    	<?php if($query->have_posts()) : while ($query->have_posts() ) : $query->the_post();?>
    		
    		<?php if( have_rows('representation') ): while( have_rows('representation') ) : the_row();?>
    		
    			<p>	
            		<?php $date_rep = get_sub_field('date_representation');?>
    			<?php echo $date_rep; ?> <?php the_title();?>	
    			</p>
    		
    		<?php endwhile; else :	endif; ?>
    		
    	<?php endwhile; else: endif; wp_reset_postdata();?>	
    
  • You cannot follow that tutorial when the relationship field is a sub field of a repeater.

    You would have to:

    1. Query ALL of the posts that could be related
    2. Loop over the posts
    3. Loop over the repeater and find the row that matches
    4. If a match is found add the Post ID to a list (array) of matching post IDs
    5. Alter the query by setting the “post_in” argument for WP_Query()

    This will likely have a significant impact on performance of the page.

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

You must be logged in to reply to this topic.