Organize Term Categories Followed by ACF Relationship Custom Fields - Wordpress

Viewed 24

I have two custom post type, the name is: "University" and "Majors".

Also, I have ACF relationship custom fields, the name is: "university_majors" with return format Post Object and Filter by Post Type: "Major".

And then, inside majors custom post type, I have taxonomy name: "major_categories".

The goal is, when I select the relationship fields inside university post type, I want it to be displayed with major categories followed by ACF relationship fields.

Example: Faculty of Computer Science and Information Technology -) Software Development -) Artificial Intelligence

and more.

Here is the screenshoot for the wp-admin:

enter image description here

Above is the university post type. enter image description here And above this is relationship custom fields.

enter image description here Above is major post type, and below is the categories:enter image description here

Actually, I can order the categories followed with the relationship post type. But, the result like this:

enter image description here

The categories not in order correctly. The Artificial Intelligence should be below software engineer not duplicate categories.

I tried many things to solve, but not found any solutions. Here is the code:

<div class="col-sm-8 position-sticky">
                                            <h2>Jurusan Kuliah di <?php the_title(); ?></h2>
                                            <p>Ada jurusan apa saja di <?php the_title(); ?>? Berikut adalah daftar jurusan yang tersedia:</p>
                                            <?php
                                                $university_majors = get_field('university_majors');
                                                $previous_category = '';

                                                foreach($university_majors as $university_major){
                                                    $categories = get_the_terms($university_major->ID, 'major_categories');
                                                    $c_terms = array();
                                                
                                                    foreach($categories as $term){
                                                        $c_terms[] = $term->name;
                                                    }
                                                    $unique_cat = array_unique($c_terms);
                                                    
                                                    ?>

                                                    <?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' => 'majors',
                                                            'orderby' => 'menu_order',
                                                            'order' => 'ASC',
                                                            'post__in' => array($university_major->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
                                                }
                                                   
                                            ?>
                                        </div>

Any help is very much appreciated. Thank you in advance, developers.

0 Answers
Related