wordpresstaxonomycustom-taxonomytaxonomy-terms

Sort your own taxonomy alphabetically


I have created a taxonomy that displays authors on a page. I created an additional "Last Name" field to sort authors by last name. Authors are displayed divided into groups according to the first letter of their surname.

         <?php
        $taxonomy = 'autorzy'; // <== Here define your custom taxonomy
        
        // Get all terms alphabetically sorted
        $terms = get_terms( array(
            'taxonomy'   => $taxonomy,
            'hide_empty' => false,
            'posts_per_page' => -1,
            'orderby' => $term_last_name,
            'order'   => 'DESC',

        ) );

        $sorted   = array(); // Initializing

        // Loop through the array of WP_Term Objects
        foreach( $terms as $term ) {
            $term_name    = $term->name;
            $term_last_name = get_field('nazwisko', $term);
            $image = get_field('obrazek_wyrozniajacy', $term);
            $term_link    = get_term_link( $term, $taxonomy );
            $first_letter = strtoupper($term_last_name[0]);
            
            // Group terms by their first starting letter
            if( ! empty($term_link) ) {
                $sorted[$first_letter][] = '<li><a href="'.$term_link.'">' .'<img src="'.$image['url'].'" />'.$term_name.'</a></li>';
            } else {
                $sorted[$first_letter][] = '<li>'.$term_name.'</li>';
            }
        }

        // Loop through grouped terms by letter to display them by letter
        foreach( $sorted as $letter => $values ) {
            echo '<div class="tax-by-letter" id="tax-letter-'.$letter.'">
            <h3 class="tax-letter-'.$letter.'">'.$letter.'</h3>
            <ul class="nav">' . implode('', $values) . '</ul>
            </div>';
        }

      ?>

The problem is that the groups created are not displayed in alphabetical order. Currently, the list is displayed as Z, R, P, C and I would like it to be displayed according to the alphabet A, B, C


Solution

  • Your are trying to orderby an undefined variable $term_last_name. Instead you should include the meta key itself with 'meta_key' => 'nazwisko' and instruct get_terms to order by this key alphabetically with 'orderby' => 'meta_value':

    $taxonomy = 'autorzy';
    
    $terms = get_terms( array(
        'taxonomy'       => $taxonomy,
        'hide_empty'     => false,
        'posts_per_page' => -1,
        'meta_key'       => 'nazwisko',
        'orderby'        => 'meta_value',
        'order'          => 'DESC',
    ));