Search code examples
wordpresscustom-taxonomy

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.


Solution

  • 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.