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',
) );
  • 1
    Replace the array('key' => 'order',), with 'meta_key' => 'order', – Sally CJ Sep 3 '18 at 11:50
  • 1
    Thanks - this seems to work. Please put as an answer so I can accept it. – user319940 Sep 3 '18 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 Sep 3 '18 at 15:22
3

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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