Home › Forums › Add-ons › Repeater Field › Reverse query "relationship within repeater"
I am building a portfolio website for a production studio. It has two custom post types, “work” and “team” to display projects and teammates. Each project in the “work” posts has an ACF repeater field called “credits” that has 1. a text field (credits__position
) and 2. a relationship field (credits__person
). In (credits__position
) the text field they type the “position” next to the selected names and in (credits__person
) the relationship field within this repeater is what allows them to select teammate names
<table>
<?php while (have_rows('credits')): the_row(); ?>
<?php $persons = get_sub_field('credits__person'); ?>
<?php if ($persons) : ?>
<?php foreach ($persons as $person) : ?>
<tr>
<th>
<?php the_sub_field('credits__position'); ?>
</th>
<th>
<a href="<?php echo get_permalink($person->ID); ?>">
<?php echo get_the_title($person->ID); ?>
</a>
</th>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>
I am trying to figure out how to reverse query this so that the list of all projects each team member is on each single-team.php
page. Currently I have this but it is not working:
<? $projects = get_posts(array(
'post_type' => 'work',
'meta_query' => array(
array(
'key' => 'credits', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if( $projects ):
foreach( $projects as $article ):
// Do something to display the articles. Each article is a WP_Post object.
// Example:
echo $article->post_title; // The post title
echo $article->post_excerpt; // The excerpt
echo get_the_post_thumbnail( $article->ID ); // The thumbnail
endforeach;
endif; ?>
Any ideas on how to solve this?
I made a regular relationship field as a test and the code above for my single-team.php
worked, so the challenge is just figuring out how to reverse query the relationship field out of a repeater
I found this thread https://support.advancedcustomfields.com/forums/topic/reverse-query-relationship-subfield-which-is-nested-in-a-repeater-field/
Here is the specific code that works for me, just in case seeing a specific helps anyone in the future
<?php
// custom filter to replace '=' with 'LIKE'
function my_posts_where( $where )
{
$where = str_replace("meta_key = 'credits_%_credits__person'", "meta_key LIKE 'credits_%_credits__person'", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// args
$args = array(
'post_type' => 'work',
'meta_query' => array(
array(
'key' => 'credits_%_credits__person',
'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(). ?>
Hello @gradywoodruff,
Thanks for your specific code. I tried replicating as you did above. But no luck for me. Is there something that might go wrong with the code?
The topic ‘Reverse query "relationship within repeater"’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.