I’m having some difficulty listing a custom post type with unique category/taxonomy headings. I have a ACF reverse relationship and currently have two articles under category 1 and one article under category 2. Next, I’m attempting to loop through each category and list them out like so:
Category 1
- article
- article
Category 2
- article
However, what the below is returning is:
Category 1
- article
Category 1
- article
Category 2
-
article
$research = get_posts(array( 'post_type' => 'research-data', 'meta_query' => array( array( 'key' => 'show_on_page', 'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. 'compare' => 'LIKE' ) ) )); ?> <?php if( $research ): ?> <h3> Research & Data</h3> <?php foreach( $research as $r ): ?> <!-- Begin custom tax loop --> <?php $categories = get_the_terms($r->ID, 'research-cats', $term_args); $c_terms = array(); foreach ( $categories as $term ) { $c_terms[] = $term->name; } $unique_cat = array_unique($c_terms); //print_r($unique_cat); ?> <strong><?php echo $unique_cat[0]; ?></strong> <ul> <?php $posts = get_posts(array( 'post_type' => 'research-data', 'orderby' => 'menu_order', 'order' => 'ASC', 'post__in' => array($r->ID), 'nopaging' => true, )); foreach($posts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li> </ul> <?php endforeach; ?> <?php endforeach; ?> <?php endif; ?>
Any thoughts? This is driving me nuts!
Without knowing how your are getting your data and what the format of $research is it would be hard but I am guessing that if you use the above with the addition of $previous_category variable it should prevent this behaviour
I ended up starting over with this and it works as expected:
http://support.advancedcustomfields.com/forums/topic/duplicate-taxonomy-with-reverse-relationship/#post-30741