Support

Account

Home Forums General Issues merge contents of 2 subfields to one result

Solved

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.

  • Is this something that goes in one’s functions.php file?

  • @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.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.