Is there any way to merge the contents of two relationship fields?
I have an event cpt and a speakers cpt.
In the event cpt I have 2 relationship fields: speakers and featured speakers.
I want I want to appear in different places for most of the site, but would like to be able to put them in the same place on one page sorted by name together.
Ie featured (andrew, david, jenny) while regular speakers has (ben, jojo)
I would like the results to be :
(andrew, ben, david, jenny, jojo)
Is this possible?
Not sure if this is the best solution or not, but it’s what I’d do. In that one place where you want them to appear together get each of the fields without formatting, this is done by specifying the 3rd argument for get_field() to tell acf not to format the value the second argument is the optional post id, which you can set to false for the current post. Merge the 2 arrays and then do a post__in query to get hem all sorted the way you want.
$all = array_merge( get_field('relationship_1', false, false), get_field('relationship_2', false, false)); $args = array( 'post_type' => 'speakers', 'posts_per_page' => -1, 'post__in' => $all, 'orderby' => 'title' ) $speakers = new WP_Query($args); // post loop
This may be close, but i may not be understanding it. My main cpt is the event, so I thought that I would be querying that post. so say i wanted to see all the speakers (both normal and frontpage) from the most recent event…
I was thinking something like this:
$query = new WP_Query( array( 'post_type' => 'events', 'posts_per_page' => '1', 'order' => 'DESC', ) ); $count = $query->post_count; $i = 1; while ( $query->have_posts() ) : $query->the_post(); $speakers = array_merge( get_field('relationship_1', false, false), get_field('relationship_2', false, false)); if($speakers): echo '<ul>'; foreach( $speakers as $speaker ): echo '<li>'.get_the_title($speaker -> ID).'</li>'; $i++; endforeach; echo '</ul>'; endif; endwhile; wp_reset_postdata(); ?>
does that make sense?
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!
© 2022 Advanced Custom Fields.