Support

Account

Home Forums General Issues ACF relationship field group by taxonomy

Solved

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; ?>	
    	
    
  • Thank you for this code! It has helped with categorization, however, I am having an issue. It is not listing the correct titles under each category. Instead it’s categorizing from the relationship field but when I do the_title(); it is repeating the curent page’s title.

    Custom post types named solutions and services. Services has taxonomy service_type attached.

    In solutions, I have created a relationship field called related_services.

    Below code is in the template for solutions page. Any idea what I might be doing wrong?

    <?php
    $avail_services = get_field( 'related_services', false, false );
    
    if ( $avail_services ) :
    	$types = get_terms( array(
    		'taxonomy' => 'service_type',
    		'hide_empty' => true,
    		'object_ids' => $avail_services
    		)
    	);
    
    	foreach( $types as $type ) :
    		$args = array(
    			'post_type'         => 'services',
    			'posts_per_page'    => -1,
    			'post__in'          => $avail_services,
    			'orderby'           => 'post__in',
    			'tax_query' => array(
    				array(
    				'taxonomy' => 'service_type',
    				'terms' => $type->slug,
    				'field' => 'slug',
    				),
    			),
    		);
    		$svc = new WP_Query($args); ?>
    		<p class="widget-title service_types"><?php echo $type->name; ?></p>
    		<ul class="sidebar-menu">
    		<?php foreach( $svc->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; ?>
  • Thank you for the code. I plugged that in and made changes as necessary and got it to work except listing the titles. It is listing the current page titles over and over instead of from the relationship field.

    Do you know where I might have gone wrong?

    $avail_services = get_field( 'related_services', false, false );
    	if ( $avail_services ) {
    		$types = get_terms( array(
    			'taxonomy' => 'service_type',
    			'hide_empty' => true,
    			'object_ids' => $avail_services
    			)
    		);
    		foreach( $types as $type ) {
    			$args = array(
    				'post_type'         => 'services',
    				'posts_per_page'    => -1,
    				'post__in'          => $avail_services,
    				'orderby'           => 'post__in',
    				'tax_query' => array(
    					array(
    					'taxonomy' => 'service_type',
    					'terms' => $type->slug,
    					'field' => 'slug',
    					),
    				),
    			);
    			$svc = new WP_Query($args);
    			echo '<h2>'.$type->name.'</h2>';
    			echo '<ul class="sidebar-menu">';
    			foreach( $svc->posts as $post ) { setup_postdata( $post );
    				echo '<li>'.get_the_title().'</li>';
    			};
    			wp_reset_postdata();
    			echo '</ul>';
    		};
    	};
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘ACF relationship field group by taxonomy’ is closed to new replies.