qbeauperin
qbeauperin

Reputation: 589

Wordpress - Order by meta_value_num not working

I have a meta on my taxonomies terms that I increment each time they are used, like this :

$count = get_term_meta($id, 'used', true);
update_term_meta($id, 'used', $count ? (intval($count) + 1) : 1);

But when I'm trying to get terms ordered by this meta, the meta_value_num doesn't seem to do anything and my terms are still ordered like if the used meta was a string.

$terms = get_terms([
    'taxonomy' => 'contract_type',
    'meta_key' => 'used',
    'order_by' => 'meta_value_num',
]);
foreach ($types as $key => $type) {
    $types[$key]->used_count = get_term_meta($type->term_id, 'used', true);
    $types[$key]->used_count_type = gettype($types[$key]->used_count);
}

And this is what is returned :

Array
(
    [0] => WP_Term Object
        (
            ...
            [used_count] => 110
            [used_count_type] => string
        )

    [1] => WP_Term Object
        (
            ...
            [used_count] => 1995
            [used_count_type] => string
        )

    [2] => WP_Term Object
        (
            ...
            [used_count] => 810
            [used_count_type] => string
        )

)

I've tried every variation of the request as I could find (with meta_query instead, meta_type, etc) without luck, so I'm suspecting that the problem is coming from the type of the meta, but I don't see what else I could do to force him to save an int instead of a string.

Any ideas ?

Upvotes: 0

Views: 475

Answers (1)

Benjamin Paap
Benjamin Paap

Reputation: 2759

I think it's orderby instead of order_by, isn't it?

Upvotes: 3

Related Questions