Support

Account

Home Forums General Issues ACF relationship field group by taxonomy

Solving

ACF relationship field group by taxonomy

  • How i can use ACF relationship filed to show output group by taxonomies.

    For example: I have post type “courses” with taxonomies “Undergraduate” & “Postgraduate”.

    If i want to show the list on homepage by using ACF relationship field. If i select course 1-6 on homepage. How to get the result like this.

    Undergraduate

    • Course 1
    • Course 3
    • Course 6

    Postgraduate

    • Course 2
    • Course 4
    • Course 5

    Want to use the relationship field, as it gave option to choose the posts to show.

  • In your relationship field you set it to return an array of post ID values. Then in your template you do 2 queries using the value for “post__in” being the value returned by the relationship field and add a tax_query to each of these queries so that you only get the posts in each term. Then you loop over the posts returned for each of these queries.

    Alternately, you create two separate relationship fields and only select the correct posts for each section to be displayed. This works out the same as the first options but you don’t need to do the queries yourself.

  • Hello, I’m interested in a sample code 🙂
    I want to to the exact same thing, but my relationship field is set to return an objet… Is it still possible ?

  • Ok I ended up with a solution, here is the code, don’t hesitate to comment!

    						<?php $spes = get_field( 'rel_spes', false, false ); if ( $spes ) : ?>
    
    	<?php 
    		$types = get_terms( array(
    		    'taxonomy' => 'job-type',
    		    'hide_empty' => true,
    		    'object_ids' => $spes 
    			)
    		);
    		
    		foreach( $types as $type ) : 
    		
    		$args = array(
    		    'post_type'         => 'job',
    		    'posts_per_page'    => -1,
    		    'post__in'          => $spes,
    		    'orderby'           => 'post__in',
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'job-type',
    					'terms' => $type->slug,
    					'field' => 'slug',
    				),
    			),							    
    		);
    		$jobs = new WP_Query($args); 
    	?>
    		<p class="widget-title job-types"><?php echo $type->name; ?></p>
    		
    		<ul class="sidebar-menu">
    			<?php foreach( $jobs->posts as $post ) : setup_postdata( $post ); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    			<?php endforeach; wp_reset_postdata(); ?>
    		</ul>
    		
    	<?php endforeach; ?>
    
    <?php endif; ?>	
    	
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.