Home › Forums › General Issues › merge contents of 2 subfields to one result
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?
well actually that seemed to work, but I had to use:
$speakers = array_merge(
get_field('relationship_1'),
get_field('relationship_2'));
instead of:
$speakers = array_merge(
get_field('relationship_1', false, false),
get_field('relationship_2', false, false));
What I had isn’t working. It brings merges the relationship fields, but doesn’t sort them as if it were one array. it presents contents of the first and then the contents of the 2nd. any wise words for me?
In order to sort the posts the way you want, after getting the merged values you would need to do a WP_Query() and specify the orderby in the query. See my original reply.
@sudo-adv that depends on what query you are using it on. In the example in this OP this appears to be a custom query run in a specific template, so it would go where the query is being done.
Hi all, and John,
I am looking to do something I think is this, but not totally sure: I need one field the user chooses one value from, that is a combination of posts of category “dogs”, and also of specific categories themselves, “dogs”, “cats”.
It seems that the Post Object and Taxonomy are the right choices, now I just need to merge them into one select. This field will be on the options page.
I am trying the code John shared before:
$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
But, trying I’m unsure how to go from
$speakers = new WP_Query($args);
to the new field of merged fields – I get syntax error with the snippet in my functions.php. Do i need to instead assign the merged array $args
into a new ACF select field? I probably create that field, and assign the array to it?
The data will be used on many different posts, and archive pages, that’s why it’s in my ACF Options page.
Thanks.
sounds like you have a similar question but not quite mine. Start a new one and tag me in it, show me the actual error and your actual post type name when you do!!
It won’t post my new question as it’s too similar to this reply. Does anyone know how to delete a previous reply I’ve made?
The topic ‘merge contents of 2 subfields to one result’ 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.