I am trying to run a reverse query on a post relation field that is within a subfield repeater. My code doesn’t return any errors but the Array always comes back empty. Any idea what I’m doing wrong? Thanks!
<?php while ( have_posts() ) : the_post(); ?> <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?> <?php $issues = get_posts(array( 'post_type' => 'product', // WooCommerce Custom Post Type 'meta_query' => array( array( 'key' => 'section_contents', //this is the post relationship field, which is in a repeater subfield of 'toc_section' 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ) ) )); print_r($issues) //testing if there's anything at all in the array. Has been returning empty. ?> <?php if( $issues ): foreach( $issues as $issues ): ?> <?php echo get_the_title( $issues->ID ); ?> <?php endforeach; endif; ?> <?php endwhile; // end of the loop. ?>
would it work if you change the 2 lines to this:
<?php if( $issues ): foreach( $issues as $issue ): ?> <?php echo get_the_title( $issue->ID ); ?>
That still returns empty. I don’t think there’s an “issue” variable anywhere.
you cant use foreach as with the same variable 😉
but your real problem is probably here:
'key' => 'section_contents', //this is the post relationship field, which is in a repeater subfield of 'toc_section'
subfield of repeater
if you have luck change this too and it works:
key' => 'toc_section_%_section_contents',
Actually, the solution is right over here: http://support.advancedcustomfields.com/forums/topic/reverse-query-relationship-subfield-which-is-nested-in-a-repeater-field/
I should have googled better before posting. 🙂
You’re right. I saw that in another post as you were replying. Thanks!
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.