0

I have the following 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 }
 } ?>   

which shows all the taxonomy terms from the projets-location taxonomy, I've added the orderby and order attributes above but STILL they're not displaying in alphabetical order at all, am I being stupid her or is there something I'm going wrong? Any suggestions would be greatly appreciated!

3

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms( array(
    'taxonomy' => 'projets-location',
    'orderby' => 'name',
    'order' => 'DESC' 
) );
2
0

As per the WordPress Codex for get_terms on this link https://developer.wordpress.org/reference/functions/get_terms/, following terms fields are accepted in order_by argument 'name', 'slug', 'term_group', 'term_id', 'id', 'description' and you are using title which is not in the accepted terms fields, so that might be the issue.

1
  • even following this they're still not ordering them at all :( May 4 '17 at 17:45
0

I didn't success to order by name using the orderby argument, so I just used a sort function ::

// order by name ASC - change > to < to order by DESC
function sortByName($a, $b) {
    return $a->name > $b->name;
}

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

foreach ( $terms as $term ) {
   ....
0

Try with 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 }
} ?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.