Satyaki
Satyaki

Reputation: 35

Is it possible to get custom taxonomy order by a meta field value?

I know how to get custom taxonomy order by id or name.

Like this -

$category = get_terms( [
    'taxonomy' => 'cmo_services_category', 
    'hide_empty' => false,
    'orderby' => 'id', 
    'order' => 'ASC', 
] );   

But I have a custom field 'order' in a custom taxonomy. Is it possible to get custom taxonomies order by meta key?

I have searched here but not getting any proper answer.Any specific answer would help me a lot. Thanks.

Upvotes: 1

Views: 4882

Answers (2)

Saddam
Saddam

Reputation: 11

$args = array( 
'taxonomy' => 'your_taxonomy_name', 
'hide_empty' => false, 
'hierarchical' => false, 
'parent' => 0, 
'meta_key' => 
'your_custom_field', 
'orderby' => 'meta_value_num', 
'order' => 'ASC' 
);

Upvotes: 1

developerme
developerme

Reputation: 1945

get_terms supports a meta_query.You can try following code with your meta key.

$args = array(
      'taxonomy' =>  'cmo_services_category',
      'orderby' =>  'meta_value_num',
      'order' =>  'ASC',
      'hide_empty' =>  false,
      'hierarchical' =>  false,
      'parent' =>  0,
      'meta_query' => array(
        'key' => 'order',
        'type' => 'NUMERIC',
     ),
    );

$terms = get_terms( $args );

This code is untested and may needs to be changed in your example. But the links should guide you to the solution.

Upvotes: 4

Related Questions