4

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 &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!

Jon Nixon
  • 155
  • 4

2 Answers2

0
$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

  • You rock. Thanks for making my day. – Jon Nixon Sep 30 '15 at 19:48
  • On second review. This approach worked on the first iteration. However, when a second category has a second result, there are duplicate headings again: https://www.dropbox.com/s/l3c5gec2pbltr78/Screenshot%202015-09-30%2020.57.52.png?dl=0 – Jon Nixon Oct 01 '15 at 00:58
  • Ah! Categories not in order I'll get something knocked up –  Oct 01 '15 at 06:05
  • @JonNixon Hello, Jon Nixon. Do you solve your problem? I still get result like your link here: https://www.dropbox.com/s/l3c5gec2pbltr78/Screenshot%202015-09-30%2020.57.52.png?dl=0 – Hendra Sep 14 '22 at 10:38
  • If you already solved it, can I get the solution please? – Hendra Sep 14 '22 at 10:39
0

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

Jon Nixon
  • 155
  • 4