Ordonarea alfabetică a termenilor taxonomiei
Am următoarea interogare:
<?php
$args = array(
'hide_empty' => false,
'orderby' => 'name', // folosește 'name' în loc de '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 }
} ?>
care afișează toți termenii taxonomiei din projets-location
. Am adăugat atributele orderby
și order
mai sus, dar TOT NU se afișează în ordine alfabetică deloc. Fac eu ceva greșit sau există o problemă? Orice sugestie ar fi foarte apreciată!

Conform WordPress Codex pentru funcția get_terms
, disponibilă la linkul
https://developer.wordpress.org/reference/functions/get_terms/, următoarele câmpuri sunt acceptate în argumentul order_by
: 'name', 'slug', 'term_group', 'term_id', 'id', 'description'
. În schimb, tu utilizezi title
, care nu se află printre câmpurile acceptate, ceea ce ar putea fi cauza problemei.

Nu am reușit să ordonez după nume folosind argumentul orderby, așa că am folosit o funcție de sortare ::
// ordonează după nume ASC - schimbă > în < pentru ordonare DESC
function sortByName($a, $b) {
return $a->name > $b->name;
}
$terms = get_terms( 'projets-location', $args );
usort($subterms, 'sortByName');
foreach ( $terms as $term ) {
....

Încearcă cu 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 }
} ?>
