Support

Account

Home Forums Add-ons Repeater Field Reverse Query Relationship subfield which is nested in a Repeater Field Reply To: Reverse Query Relationship subfield which is nested in a Repeater Field

  • Hi @eugenenyawara

    This is a great question, and one which can be achieved by reading about querying posts via a sub field value here:
    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/#example-5

    I have done some copy / paste and put together a template for you. Just replace {$repeater_field_name} with your repeater field name, and {$sub_field_name} with your sub field name, and it should work! Fingers crossed:

    
    <?php 
     
    // custom filter to replace '=' with 'LIKE'
    function my_posts_where( $where )
    {
    	$where = str_replace("meta_key = '{$repeater_field_name}_%_{$sub_field_name}'", "meta_key LIKE '{$repeater_field_name}_%_{$sub_field_name}'", $where);
     
    	return $where;
    }
     
    add_filter('posts_where', 'my_posts_where');
     
    // args
    $args = array(
    	'post_type'	=> 'whose-posts-we-want-to-display',
    	'meta_query' => array(
    		array(
    			'key' => '{$repeater_field_name}_%_{$sub_field_name}',
    			'value' => '"' . get_the_ID() . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    

    Thanks
    E