admin管理员组

文章数量:1287593

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!

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!

Share Improve this question asked May 4, 2017 at 17:05 user1374796user1374796 39511 gold badges18 silver badges30 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 4

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' 
) );

As per the WordPress Codex for get_terms on this link https://developer.wordpress/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.

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 ) {
   ....

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

本文标签: wp queryOrder taxonomy terms in alphabetical order