Y
Y
Yastr2018-06-06 19:58:51
WordPress
Yastr, 2018-06-06 19:58:51

How to sort array of taxonomy terms by field value?

Good evening!
I get taxonomy terms.

add_action('init', 'get_proizv_terms');
function get_proizv_terms() {
    $terms = get_terms( array(
        'hide_empty'  => 0,  
        'orderby'     => 'meta_value_num',
        'order'       => 'ASC',
        'taxonomy'    => 'pa_obem-resivera-l',
        'pad_counts'  => 1
    ));
}

Firstly, it is not sorted by meta_value_num, sorting occurs, as I understand it, by name, and not by its digital value.
5b1812c3238e7308677417.png
Secondly, I need to process it as an array, I try to convert it with
->to_array(); I get an error.
https://wp-kama.ru/function/WP_Post::to_array
Please tell me how can I convert it to an array and sort it normally by the digital value name.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yastr, 2018-06-07
@Yastr

With Anatoly's help, I came up with a simple solution.

function attrName_to_termMetaOrder() {
    $terms = get_terms(array(
        'hide_empty'  => 0, 
        'order'       => 'ASC',
        'taxonomy'    => 'pa_proizvoditelnost',
        'pad_counts'  => 1
    ));

    if (!empty($terms) && !is_wp_error($terms)){
        foreach ($terms as $term) {
            update_term_meta($term->term_id, 'order_pa_proizvoditelnost', intval($term->name), "");
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question