1

I'm doing a get_terms() query that I'm trying to order by a custom term meta. The custom term meta key is 'order' which as a numeric value (between 1 and 10). I have tried the following, but the order doesn't seem to follow the meta value - any pointers is appreciated.

$type_terms = get_terms( 'type', array(
    'hide_empty' => false,
    array(
        'key' => 'order',
    ),
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
) );
3
  • 1
    Replace the array('key' => 'order',), with 'meta_key' => 'order',
    – Sally CJ
    Commented Sep 3, 2018 at 11:50
  • 1
    Thanks - this seems to work. Please put as an answer so I can accept it.
    – user319940
    Commented Sep 3, 2018 at 13:28
  • I've done that, which you already know. I edited the answer so that it would be more clarified to other users. =)
    – Sally CJ
    Commented Sep 3, 2018 at 15:22

1 Answer 1

4

the order doesn't seem to follow the meta value

Yes, and it's because you set the meta key like this: (which doesn't actually set the meta key)

array(
  'key' => 'order',
),

The proper way is by using the meta_key parameter:

'meta_key' => 'order'

So the full code would be:

$type_terms = get_terms( 'type', array(
    'hide_empty' => false,
    'meta_key' => 'order',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
) );

For more information, please refer to WP_Term_Query::__construct(), which is used by the get_terms() function.

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.