1

I am running into an issue when trying to display my custom taxonomy alphabetically. To break down my code to easy understand what I am doing is as followed.

I have two separate custom taxonomy "juryyear" and "jury_category". The reason for this is because I have a jury page with multiple jury categories and year. Each year you selected will display the jury members that are assigned to that year. Once you click on a jury member from a particular year it displays the jury member and the list of jury category for that year.

Example Year 2017

Category 1

Category 2

Category 3

Category 4

Category 5

Category 6

Each jury member can have multiple categories assign to them (a primary and sub categories). The issue I am having is listing the category alphabetically. What is happening now is they are being displayed based on the menu order of the jury member.

For example

Category 1

Category sub 1

Category sub 2

Category 3

Category 5

Category 6

Category 4

Category 6

What am I doing wrong with the below code to properly display all the categories alphabetically?

 <?php 
        $custom_taxterms = wp_get_object_terms( $post->ID, 'juryyear', array('fields' => 'ids') );
        $args = array(
            'post_type'         => $current_post->post_type,
            'posts_per_page'    => 99,
            'tax_query'         => array(
                array(
                    'taxonomy'  => 'juryyear',
                    'field'     => 'id',
                    'terms'     => $custom_taxterms
                )
            ),
            //'post__not_in' => array ($post->ID),
        );
        $related_items = new WP_Query( $args );
        if ($related_items->have_posts()) :
        echo '<ul>';
        while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
    <?php
        $media_attrs = wp_get_post_terms( $post->ID, 'jury_category', 
            array(
                'orderby'   => 'title',
                'order'     => 'ASC'
        ) );
        if (!empty($media_attrs)){ 
        foreach($media_attrs as $attr){
        echo '<li><a href="' .get_permalink( $post->ID ).'">';
        echo $attr->name  . '</li></a>';}    
    ?>
    <?php } ?>
    <?php endwhile; echo '</ul>'; endif; wp_reset_postdata();?>
  • I don't understand this code or the usage of WP_Query. Where are $post and $current_post coming from? That said, simply as a PHP matter, if you are somehow producing the list of categories and sub-categories as depicted, then, instead of outputting them with your <ul> and <li> tags, you could or should be able to capture them in an array instead, with title and ID, sort them by title, iterate (foreach) over the new, sorted array and THEN output the list tags, using the IDs to get whatever other data you need. But, like I said, I really don't understand how this is working. – CK MacLeod Apr 16 '17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.