Support

Account

Home Forums General Issues ACF relationship field group by taxonomy Reply To: ACF relationship field group by taxonomy

  • 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>';
    		};
    	};