get_terms() orderby name is not working - wordpress
Asked Answered
D

7

8

I'm using wordpress, want first-level taxonomy terms to be ordered by name but below code is not giving me desired result. Here is my code:

$args = array(
    'taxonomy' => 'tax-category', 
    'hide_empty' => 0,
    'hierarchical' => 1,
    'parent' => 0,
    'orderby'=>'name',
    'order' => 'DESC',
    'fields' => 'all',
);
$rs_terms = get_terms('tax-category', $args);

When I'm adding below php sorting, it works perfectly. But want to know why wordpress's default sorting is not working properly:

usort($rs_terms, function($a, $b){
    return strcmp($a->name, $b->name);
});
Dzungaria answered 6/7, 2017 at 7:47 Comment(6)
did you try 'order' => 'ASC', instead of DESC ?Rearm
yes same wrong result @RearmDzungaria
Hi it is working for me.Rearm
Here is the code which worked for me, <?php $args = array( 'taxonomy' => 'category', 'hide_empty' => 0, 'hierarchical' => 1, 'parent' => 0, 'orderby'=>'name', 'order' => 'ASC', 'fields' => 'all', ); $rs_terms = get_terms('category', $args); echo '<pre>',print_r($rs_terms,1),'</pre>'; ?>Rearm
I've just tried to put your code but it is still ordering category names in wrong way (i.e. D, B but it should be B,D) @RearmDzungaria
how you displayed $rs_terms?Rearm
O
16

Showed up here with the same problem, and like others mentioned, the culprit was a plugin related to taxonomy sorting. Category Order and Taxonomy Terms Order, in my case. I deactivated it, and my terms list popped into order.

Opulence answered 14/3, 2019 at 18:6 Comment(5)
This was my problem as well. Hope there's a way to override this without uninstalling the plugin. Opened a support ticket about this issue: wordpress.org/support/topic/order-terms-by-countSempach
Per that issue, just add 'ignore_term_order' => true to your array. Works for me.Bullpen
You can also turn off "Auto Sort" instead in the Plugin settings page.Fervent
thank you! omg... hours of searching lost in a snapPedal
This was the clue — thank you! I forget about long-running plugins. Turning off "Auto Sort" was sufficient to fix the issue for me.Additive
H
4

Your code should work fine. I had for same problem and I found a hook in my plugin that changed 'orderby' value. It might be the same case.
I suggest you look for a filter function hooked to get_terms() in your plugin/theme.

Possible hooks:

  • terms_clauses
  • get_terms_orderby
  • get_terms_args

EDIT: Before you go scanning the hooks you should try adding 'menu_order' => false to your args, it might do the job for you. There are taxonomies with manual drag&drop sorting (menu_order), so you just need to unable it.

Handstand answered 22/10, 2017 at 9:18 Comment(0)
E
3

I just tested your code on my localhost and it works.

                    'orderby'           => 'name', 
                    'order'             => 'ASC',
                    'hide_empty'        => false, 
                    'fields'            => 'all', 
                    'parent'            => 0,
                    'hierarchical'      => true, 
                    'child_of'          => 0,
                    'childless'         => false,
                    'pad_counts'        => false, 
                    'cache_domain'      => 'core'
Enchiridion answered 6/7, 2017 at 9:21 Comment(0)
E
1

Same issue here, I confirm what Cory was mentioning, the Category Order and Taxonomy Terms Order does change the search order. I was able to work around with removing the plugin filter just for my request with the code below.

remove_filter('terms_clauses', 'TO_apply_order_filter', 10, 3);

//do your stuff here...

add_filter('terms_clauses', 'TO_apply_order_filter', 10, 3);
Eagre answered 29/7, 2021 at 8:59 Comment(0)
M
0

You may also check your PHP modules installed. Assuming you're on PHP 7.x, make sure no APC or APCu modules are loaded.

php -m | grep -i apc

Should come with no output.

Make answered 28/3, 2018 at 1:34 Comment(0)
R
0

I had the same problem. I was using the plugin Intuitive Custom Post Order that does ordering by drag and drop in WordPress admin panel. This was overriding my "orderby" in get_terms(), so I changed ordering from admin panel. If you use any similar plugin it maybe overrides the "orderby".

Racy answered 16/1, 2019 at 9:37 Comment(0)
B
0

Try with wpdb

<?php
global $wpdb;
$rs_terms = $wpdb->get_results( "
    SELECT
        t.*
    FROM
        {$wpdb->prefix}term_taxonomy AS tt
    INNER JOIN
        {$wpdb->prefix}terms AS t
        ON t.term_id = tt.term_id
    WHERE
        tt.taxonomy = 'tax-category'
        AND tt.parent = '0'
    ORDER BY
        t.name DESC
" );
?>
Bronder answered 28/5, 2021 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.