I have a custom taxonomy called prod-cat

I want to order the output in the template by number, so I added a term_meta to the taxonomy like this:

add_action( 'prod-cat_add_form_fields', 'add_feature_group_field', 10, 2 );
function add_feature_group_field($taxonomy) {
    ?>
    <div class="form-field term-order-wrap">
        <label for="term-order">Order</label>
        <input type="text" name="wm-cat-prod-order" />
    </div>
    <?php
}

And then:

add_action( 'created_prod-cat', 'save_feature_meta', 10, 2 );
function save_feature_meta( $term_id, $tt_id ){
    if( isset( $_POST['wm-cat-prod-order'] ) && '' !== $_POST['wm-cat-prod-order'] ){
        add_term_meta( $term_id, 'wm-cat-prod-order', $_POST['wm-cat-prod-order'], true );
    }
}

I have the term_meta working, It's getting saved. Then in the template I do this:

$args = array(
    'taxonomy'      =>  'categoria-de-productos',
    'orderby'       =>  'wm-cat-prod-order',
    'order'         =>  'ASC',
    'hide_empty'    =>  false,
    'hierarchical'  =>  false,
    'parent'        =>  0,
);

$terms = get_terms( $args );

But I can't get it to orderby the "wm-cat-prod-order" meta. Anyone on this? Thanks

up vote 7 down vote accepted

get_terms supports a meta_query which calls a new WP_Meta_Query parameter as you can see here. To query your terms with your desired meta, you could change your function call to something like this:

$args = array(
  'taxonomy' => 'categoria-de-productos',
  'orderby' => 'meta_value_num',
  'order' => 'ASC',
  'hide_empty' => false,
  'hierarchical' => false,
  'parent' => 0,
  'meta_query' => [[
    'key' => 'wm-cat-prod-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.

  • That did the trick. Thanks man. – Ariel Nonamë Nov 23 '16 at 17:30
  • 1
    This doesn't work on categories + acf – vladkras May 30 '17 at 4:56
  • What exactly do you want to achieve @vladkras? – Fabian Marz May 30 '17 at 8:34

for me, I made a custom taxonomy and in that custom taxonomy I had a custom meta. I wanted to have in the admin backend a column and made it sortable. to make sortable work for a custom taxonomy in a custom meta I did this.

https://pastebin.com/vr2sCKzX

public function pre_get_terms( $query ) {
$meta_query_args = array(
    'relation' => 'AND', // Optional, defaults to "AND"
    array(
        'key'     => 'order_index',
        'value'   => 0,
        'compare' => '>='
    )
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$query->meta_query = $meta_query;
$query->orderby = 'position_clause';

} I found the answer in this link https://core.trac.wordpress.org/ticket/34996

I just had to adapt the answer provided in the comments by @eherman24

$args = array(

    'taxonomy'      => 'MY_TAX',
    'meta_key'      => 'ordem',
    'meta_compare'  => 'NUMERIC',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC',
    'hide_empty'    => false,
);

$the_query = new WP_Term_Query($args);

foreach ( $the_query->get_terms() as $term )
{
    ...
}
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Aug 29 at 17:46

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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