10

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

4 Answers 4

17

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.

3
  • 1
    This doesn't work on categories + acf
    – vladkras
    May 30, 2017 at 4:56
  • What exactly do you want to achieve @vladkras? May 30, 2017 at 8:34
  • 1
    You can also negate the meta_query part of your arguments by simply adding in 'meta_key'=>'wm-cat-prod-order' whilst making sure that you still keep the 'orderby' => 'meta_value_num' part in
    – Frits
    Dec 3, 2019 at 7:43
4
$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 )
{
    ...
}
2
  • 1
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Aug 29, 2018 at 17:46
  • 1
    This works with custom post type and is the simple solution, just change the meta_key same as field_name (note this is a number)
    – user145078
    Feb 3, 2019 at 9:53
3

I was having a rough time with this as well, and I created my meta field with ACF. This is what I did to get it to work (removed some properties for brevity):

$args = array(
    'taxonomy'      => 'categoria-de-productos',
    'order'         => 'ASC',
    'orderby'       => 'meta_value_num',//Treat the meta value as numeric
    'meta_key'      => 'wm-cat-prod-order'//Meta key
);
$terms_query = new WP_Term_Query( $args );
if( ! empty( $terms_query->terms ) ) {
   foreach( $terms_query->terms as $term ) {
      //Do stuff, $term is a WP_Term object
    }
}

One of the things I usually do is define a fallback, like 'meta_value_num term_id' to use term_id if the values for wm-cat-prod-order are all the same, but this broke it entirely and produced unexpected results. It only worked if 'meta_value_num' was the only value for orderby.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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