0

I make a custom field "term_order" in wp_terms and i make a functionality to re-order the custom taxonomy. When i drag and drop the terms it save the re-order in "term_order" field successfully, but when i write and execute the "orderby => term_order" query it does not work.

Here is my code:

<?php
     $foo_parent_ID = 0;

     $foo_args = array(
                'orderby' => 'term_order',
                'order' => 'ASC',
                'hide_empty' => false,
                'parent' => $foo_parent_ID
     );

     $foo_terms = get_terms('foo_cat', $foo_args);

     if($foo_terms){
?>
       <ul id="foo_order_sortable" class="foo_admin_order">
       <?php
            foreach($foo_terms as $foo_term) :
       ?>
            <li id="foo_parent_id_<?php echo $foo_term->term_id; ?>" class="lineitem <?php echo ($i % 2 == 0 ? 'alternate ' : ''); ?>ui-state-default">
                <?php echo $foo_term->name.' _ id='.$foo_term->term_id.' ==> order-'.$foo_term->term_order; ?>
            </li>
       <?php
             endforeach;
       ?>
       </ul>

It shows me orderby Id, where is the mistake i dont understand.

deemi-D-nadeem
  • 2,343
  • 3
  • 30
  • 71

1 Answers1

0

Ok after many searches i found the solution.

When you make custom field in any table of the wordpress database, you just add this filter:

add_filter('get_terms_orderby', 'function name', 10, 2);

Here is my Solution:

function foo_tax_order($orderby, $args){

    $kbe_tax = "foo_cat";

    if($args['orderby'] == 'term_order'){
        return 't.term_order';
    }elseif($kbe_tax == 1 && !isset($_GET['orderby'])){
        return 't.term_order';
    }else{
        return $orderby;
    }
}
add_filter('get_terms_orderby', 'foo_tax_order', 10, 2);
deemi-D-nadeem
  • 2,343
  • 3
  • 30
  • 71