admin管理员组

文章数量:1327524

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

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

Share Improve this question asked Nov 22, 2016 at 2:55 Ariel NonamëAriel Nonamë 1031 gold badge1 silver badge5 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 18

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.

$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 )
{
    ...
}

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.

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/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/ticket/34996

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

本文标签: Order getterms by term meta