I've experimented and researched for hours trying to find a way to get this to work, but I'm just not having any luck.
So I have a couple different custom post types that need to co-mingle a bit. Let me try and explain it as clear and economically as possible.
There are 2 custom post types, Messages & Campuses. (this is a large multi-campus church website). Each Message post will have some repeater rows based on Campuses (through the Taxonomy Field), and each Campus post will be trying to grab the latest Message post, and pull the specific "campus" repeater row, and the same field values from that row.
The ACF repeater section for the Message single post... (we're choosing one of the Campus posts, then manually entering the speaker name and the message URL)
So at the moment, I'm inside of a "Campus" single post, trying to query and get the latest Message post, which is working. But then I'm trying to target and pull in ONLY the values of a specific repeater row INSIDE of the Message post. The repeater fields in the Message are a Campus Select (select box based on the Post Object field (on the "campus" post-type), and 2 different text fields.
To further illustrate... the Message post has the 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) ... and there will be a dynamic number of repeaters for the multiple campuses.
So what I'm trying to do is as follows... Inside of my Campus post - "Troy" for example - I want to grab the latest Message post, go into the repeaters and find the row with the "Campus Select" value. I want to return the Message Speaker (Danny Cox), and Message URL (url A) from ONLY that row. I don't need anything from the other rows.
I have 2 repeater rows in my current "latest message" and here's my 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 to grab the "troy" value "campus selector" instead of getting that value dynamically, based on my current "campus" location...
The query is returning both: "Cliff Johnson Danny Cox" (as I have 2 repeater rows).
How can I return ONLY: " Troy, Danny Cox, url A " in my query?
. . . == 'troy'
with that post name. Finally remove the corresponding else which produces the same output. I would however look at a couple of alternatives. 1) consider a different structure or 2) use a custom DB query (codex.wordpress.org/Class_Reference/wpdb) – Nathan Dawson