wordpress - How to retrieve specific ACF repeater row with matching sub-field value -
i've experimented , researched hours trying find way work, i'm not having luck.
so have couple different custom post types need co-mingle bit. let me try , explain clear , economically possible.
there 2 custom post types, messages & campuses. (this large multi-campus church website). each message post have repeater rows based on campuses (through taxonomy field), , each campus post trying grab latest message post, , pull specific "campus" repeater row, , same field values row.
the acf repeater section message single post... (we're choosing 1 of campus posts, manually entering speaker name , message url)
so @ moment, i'm inside of "campus" single post, trying query , latest message post, working. i'm trying target , pull in values of specific repeater row inside of message post. repeater fields in message campus select (select box based on post object field (on "campus" post-type), , 2 different text fields.
to further illustrate... message post has following repeater data: repeater row 1: campus select - troy (post), message speaker - danny cox, message url - (url a) repeater row 2: campus select - birmingham (post), message speaker - cliff johnson, message url - (url b) ... , there dynamic number of repeaters multiple campuses.
so i'm trying follows... inside of campus post - "troy" example - want grab latest message post, go repeaters , find row "campus select" value. want return message speaker (danny cox), , message url (url a) row. don't need other rows.
i have 2 repeater rows in current "latest message" , here's current query:
<?php $args = array( 'post_type' => 'messages', 'posts_per_page' => 1 ); $latestmessagequery = new wp_query($args); if( $latestmessagequery->have_posts() ) { ?> <?php while ($latestmessagequery->have_posts()) : $latestmessagequery->the_post(); ?> <?php if( have_rows('campus_message') ): ?> <?php while( have_rows('campus_message') ): the_row(); ?> <?php if( get_sub_field('campus_selector') == 'troy' ): ?> <?php the_sub_field('message_speaker'); ?> <?php else: ?> <?php the_sub_field('message_speaker'); ?> <?php endif; ?> <?php endwhile; ?> <?php endif; ?> <?php endwhile; ?> <?php } wp_reset_query(); ?>
for troubleshooting, i'm manually trying grab "troy" value "campus selector" instead of getting value dynamically, based on current "campus" location...
the query returning both: "cliff johnson danny cox" (as have 2 repeater rows).
how can return only: " troy, danny cox, url " in query?
i got acf support on issue, , following code sent me got asking for.
<?php $args = array( 'post_type' => 'messages', 'posts_per_page' => 1 ); $current_campus_id = get_the_id(); $latestmessagequery = new wp_query($args); if( $latestmessagequery->have_posts() ) { while ($latestmessagequery->have_posts()) : $latestmessagequery->the_post(); if( have_rows('campus_message') ): while( have_rows('campus_message') ): the_row(); if( get_sub_field('campus_selector') == $current_campus_id ): the_sub_field('message_speaker'); the_sub_field('message_url'); break; endif; endwhile; endif; endwhile; } wp_reset_query(); ?>
Comments
Post a Comment