Skip to main content
2 of 6
Added admin orderby as extra information.
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

SOLUTION

Thanks to /u/G.M.

/** Add New Field To Category **/
function extra_category_fields( $tag ) {
    $t_id = $tag->term_id;
    $cat_meta = get_option( "category_$t_id" );
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="meta-order"><?php _e('Category Order'); ?></label></th>
    <td>
        <div id="catOrder">
            <input type="text" name="cat_meta[order]" size="3" style="width:5%;" value="<?php echo (isset($cat_meta['order'])) ? $cat_meta['order'] : '0'; ?>" />
        </div>
        <span class="description"><?php _e('Categories are ordered Smallest to Largest'); ?></span>
    </td> 
</tr>
<?php
}
add_action('protax_edit_form_fields','extra_category_fields');  

/** Save Category Meta **/
function save_extra_category_fileds( $term_id ) {
    global $wpdb;
    if ( isset( $_POST['cat_meta'] ) ) {
        $t_id = $term_id;
        $cat_meta = get_option( "category_$t_id");
        $cat_keys = array_keys($_POST['cat_meta']);
            foreach ($cat_keys as $key){
            if (isset($_POST['cat_meta'][$key])){
                $cat_meta[$key] = $_POST['cat_meta'][$key];
            }
        }
        update_option( "category_$t_id", $cat_meta );
        $wpdb->update($wpdb->terms, array('term_group' => $cat_meta['order']), array('term_id'=>$term_id));
    }
}
add_action ( 'edited_protax', 'save_extra_category_fileds');

Calling it:

$procats = get_categories(array('taxonomy' => 'protax', 'hide_empty' => 0, 'orderby' => 'term_group'));

Edit - You're also able to order them in the admin panel by using this filter:

function change_term_order( $orderby, $args, $taxonomies ) {
    if ( is_admin() && 'protax' !== $taxonomies[0] )
        return $args;
    
    $orderby = 'term_group';
    $args['order'] = 'ASC';
}
add_filter( 'get_terms_orderby', 'change_term_order', 10, 3 );
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175