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
services. Services has taxonomy
solutions, I have created a relationship field called
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; ?>
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!
© 2022 Advanced Custom Fields.