2

I have a custom taxonomy called "edition". Each edition has a meta key "_edition" that stores an incremental number from 1 to N. So, if I have an edition called "Edition 13", it will store "_edition" meta key with value of 13.

My question is: I'm trying to order my terms in admin with this "_edition" meta key. To achieve that, I'm trying to use the "get_terms_args" filter, this way:

add_filter("get_terms_args", "MyTheme_GetTermsArgs", 10, 2);

function MyTheme_GetTermsArgs($args, $taxonomies)
{
    if(is_admin() && in_array("edition", $taxonomies)){
        $args['orderby'] = "meta_value_num";
        $args['meta_key'] = "_edition";
        $args['order'] = "DESC";
    }
    
    return $args;
}

But the resulting order is very strange. There are around 123 editions, and the result ordering is

1, 2, 3, [...], 8, 9, 10, 100, 101, 102, 103, [...], 109, 11, 110, 111, 112, 113, [...], 118, 119, 12, 120, 121, 122, 123, 13, 14, 15, 16 [...].

The expected is a "normal" ordering from 1 to 123. I've already tried changing "orderby" to "meta_value", specifying an "meta_type". All without success.

Can someone help me?

Thanks!

2
  • What you are seeing there is alphabetical ordering. Are you sure no other filters are running on get_terms_args?
    – vancoder
    Commented Aug 8, 2022 at 17:14
  • @vancoder I've searched my whole project for anything else calling the get_terms_args filter, and doesn't found anything. Also, I've tested setting the add_filter priority to PHP_INT_MAX, to make sure it's the last thing done in this filter, and stills not working.
    – RBFraphael
    Commented Aug 8, 2022 at 18:23

1 Answer 1

2

After a lot of searching and tests... Finally I've found a solution.

Instead using get_terms_args filter, I've changed to parse_term_query action. My resulting code looks this way now:

add_action("parse_term_query", "MyTheme_ParseTermQuery", PHP_INT_MAX, 1);

function MyTheme_ParseTermQuery(&$query)
{
    $taxonomy = $query->query_vars['taxonomy'][0];
    if($taxonomy == "edition"){
        $query->query_vars['order'] = "ASC";
        $query->query_vars['orderby'] = "meta_value_num";
        $query->query_vars['meta_query'] = [[
            'key' => "_edition",
            'type' => "NUMERIC"
        ]];
    }
}

Now it's ordering as expected.

1
  • 2
    You saved my life man. Thank you, I have been pulling my hair out.
    – noahmason
    Commented Oct 27, 2022 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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