'Is it possible to sort terms by get_term_meta in wordpress

Lets see an example of my args:

$args = array( 
    'orderby' .     => $orderby,
    'number'        => $per_page,  
    'offset'        => $offset,
    'exclude'       => array(),    
    'exclude_tree'  => array(), 
    'include'       => array(),
    'fields'        => 'all', 
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'pad_counts'    => false, 
    'offset'        => $offset, 
    'terms'         => $term->slug,
    'cache_domain'  => 'core' 
);

If I have a term_meta like this one which is saved in database

$yellow = get_term_meta($term->term_id,'product_yellow',true);

Is it possible to sort only taxonomies with yellow term_meta ?



Solution 1:[1]

Yes, it's possible.

$args = array(  
   'taxonomy' => 'post_tag',
   'meta_key' => 'product_yellow',
   'orderby' => 'meta_value', // use 'meta_value_num' if the value type of this meta is numeric.
   'order' => 'DESC',
);
$terms = get_terms($args);

Solution 2:[2]

Do you mean something like this?

$args = array(
    'meta_query' => array(
        array(
           'key'       => $term->term_id,
           'value'     => 'product_yellow',
           'compare'   => 'LIKE'
        )
    )
);

That will limit the scope of your query to a specific meta value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cl0udSt0ne
Solution 2 Tim Sheehan