Duplicate Term with get_the_terms()

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

Read More
  • 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 &amp; 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!

Related posts

2 comments

  1. $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'
                    )
                )
            ));
    $previous_category = '';
            ?>
            <?php if( $research ): ?>
            <h3> Research &amp; 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);
    
            ?>
    
            <?php if($previous_category !== $unique_cat[0]) { ?><strong><?php $previous_category = $unique_cat[0]; echo $unique_cat[0]; ?></strong><?php } ?>
    
            <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; ?>
    

    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

Comments are closed.