Ordinare i termini della tassonomia in ordine alfabetico

4 mag 2017, 20:05:15
Visualizzazioni: 23.7K
Voti: 1

Ho la seguente query:

<?php 
$args = array(
    'hide_empty' => false, 
    'orderby' => 'title',
    'order' => 'DESC' 
);
$terms = get_terms( 'projets-location', $args );
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     foreach ( $terms as $term ) { ?>   

        <h5 id="<?php echo $term->slug; ?>" class="filter-menu-item" data-filter=".<?php echo $term->slug; ?>">
            <strong><?php echo $term->name; ?></strong>
        </h5>

    <?php }
 } ?>   

che mostra tutti i termini della tassonomia projets-location, ho aggiunto gli attributi orderby e order sopra ma NON vengono ancora visualizzati in ordine alfabetico, sto facendo qualcosa di sbagliato o c'è qualche errore? Qualsiasi suggerimento sarebbe molto apprezzato!

0
Tutte le risposte alla domanda 4
2

A partire dalla versione 4.5.0, le tassonomie dovrebbero essere passate tramite l'argomento 'taxonomy' nell'array $args:

$terms = get_terms( array(
    'taxonomy' => 'projets-location',
    'orderby' => 'name',
    'order' => 'DESC' 
) );
4 mag 2017 21:53:36
Commenti

Ho appena provato ma non ha funzionato?

user1374796 user1374796
4 mag 2017 23:21:08

non funziona su WP 5.3

niki niki
28 mar 2020 08:13:02
1

Come indicato nel Codex di WordPress per get_terms su questo link https://developer.wordpress.org/reference/functions/get_terms/, i seguenti campi dei termini sono accettati nell'argomento order_by: 'name', 'slug', 'term_group', 'term_id', 'id', 'description' e stai utilizzando title che non è tra i campi accettati, quindi questo potrebbe essere il problema.

4 mag 2017 20:20:05
Commenti

anche seguendo questo non li ordinano affatto :(

user1374796 user1374796
4 mag 2017 20:45:38
0

Non sono riuscito a ordinare per nome utilizzando l'argomento orderby, quindi ho semplicemente usato una funzione di ordinamento ::

// ordina per nome ASC - cambia > con < per ordinare in DESC
function sortByName($a, $b) {
    return $a->name > $b->name;
}

$terms = get_terms( 'projets-location', $args );
usort($subterms, 'sortByName');

foreach ( $terms as $term ) {
   ....
21 giu 2018 12:38:25
0

Prova con wpdb

<?php
global $wpdb;
$terms = $wpdb->get_results( "
    SELECT
        t.name,
        t.slug
    FROM
        {$wpdb->prefix}term_taxonomy AS tt
    INNER JOIN
        {$wpdb->prefix}terms AS t
        ON t.term_id = tt.term_id
    WHERE
        tt.taxonomy = 'projets-location'
    ORDER BY
        t.name DESC
" );

if ( !empty( $terms ) && !is_wp_error( $terms ) ){
    foreach ( $terms as $term ) { ?>

        <h5 id="<?php echo $term->slug; ?>" class="filter-menu-item" data-filter=".<?php echo $term->slug; ?>">
            <strong><?php echo $term->name; ?></strong>
        </h5>

    <?php }
} ?>
28 mag 2021 21:33:50