Skip to main content
The function change_term_order should return $orderby, not the $args array
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

Edit — The function change_term_order should return $orderby, not the $args array

Edit — The function change_term_order should return $orderby, not the $args array

The function change_term_order should return $orderby, not the $args array
Source Link
function change_term_order( $orderby, $args, $taxonomies ) {
    if ( is_admin() && 'protax' !== $taxonomies[0] )
        return $args;$orderby;
    
    $orderby = 'term_group';
    $args['order'] = 'ASC';
    return $orderby;
}
add_filter( 'get_terms_orderby', 'change_term_order', 10, 3 );

Edit — The function change_term_order should return $orderby, not the $args array

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

Edit — The function change_term_order should return $orderby, not the $args array

Updated code, added more documentation.
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

SOLUTION - Updated: 03/03/2015

  - Thanks to /u/G.M.

The below adds a new field to the Term Edit Page and saves the value into the un-used ( for the moment anyway ) term_group field which can then be used to order terms. In the below hooks ( actions ) you'll need to replace TAXONOMY_SLUG with your actual taxonomy slug.

/**
 * Append Fields To Term Edit Page
 * @param Term Object $term
 * @param string $taxonomy
 */
function term_order_field( $term, $taxonomy ) {
  ?>
    <tr class="form-field">
        <th scope="row" valign="top">
            <label for="meta-order"><?php _e( 'Category Order' ); ?></label>
        </th>
        <td>
            <input type="text" name="_term_order" size="3" style="width:5%;" value="<?php echo ( ! empty( $term->term_group ) ) ? $term->term_group : '0'; ?>" />
            <span class="description"><?php _e( 'Categories are ordered Smallest to Largest' ); ?></span>
        </td> 
    </tr>
  <?php
}
add_action( 'TAXONOMY_SLUG_edit_form_fields', 'term_order_field', 10, 2 );

/**
 * Save Term Order
 * @param int $term_id
 */
function save_term_order( $term_id ) {
    global $wpdb;
    
    if( isset( $_POST['_term_order'] ) ) {
        $wpdb->update( 
            $wpdb->terms,
            array(
                'term_group' => $_POST['_term_order']
            ),
            array( 
                'term_id'    => $term_id
            )
        );
    }
} // END Function
add_action( 'edited_TAXONOMY_SLUG', 'save_term_order' );

Add Term Order cell to Term List

/**
 * Add Column To Show 'Term Order' Field
 * @param array $columns
 * @return array $columns
 */
function term_order_header( $columns ) {
    $columns['order'] = '<center>Order</center>';
    return $columns;
}
add_filter( 'manage_edit-TAXONOMY_SLUG_columns', 'term_order_header' );

/**
 * Give 'Term Order' Column A Value `term_group`
 * @param string $empty
 * @param string $col_name
 * @param int $term_id
 * @return string
 */
function term_order_header_val( $empty = '', $col_name, $term_id ) {
    if( isset( $_GET['taxonomy'] ) && 'order' == $col_name ) {
        $term = get_term( $term_id, $_GET['taxonomy'] );
        return "<center>{$term->term_group}</center>";
    } else {
        return '0';
    }
}
add_filter( 'manage_TAXONOMY_SLUG_custom_column', 'term_order_header_val', 10, 3 );

Hook Documentation


Below is an out-dated solution that works better if you have multiple Term Meta Values you would like to save. If you only need to save Term Order, the above solution is best.

SOLUTION

  Thanks to /u/G.M.

SOLUTION - Updated: 03/03/2015 - Thanks to /u/G.M.

The below adds a new field to the Term Edit Page and saves the value into the un-used ( for the moment anyway ) term_group field which can then be used to order terms. In the below hooks ( actions ) you'll need to replace TAXONOMY_SLUG with your actual taxonomy slug.

/**
 * Append Fields To Term Edit Page
 * @param Term Object $term
 * @param string $taxonomy
 */
function term_order_field( $term, $taxonomy ) {
  ?>
    <tr class="form-field">
        <th scope="row" valign="top">
            <label for="meta-order"><?php _e( 'Category Order' ); ?></label>
        </th>
        <td>
            <input type="text" name="_term_order" size="3" style="width:5%;" value="<?php echo ( ! empty( $term->term_group ) ) ? $term->term_group : '0'; ?>" />
            <span class="description"><?php _e( 'Categories are ordered Smallest to Largest' ); ?></span>
        </td> 
    </tr>
  <?php
}
add_action( 'TAXONOMY_SLUG_edit_form_fields', 'term_order_field', 10, 2 );

/**
 * Save Term Order
 * @param int $term_id
 */
function save_term_order( $term_id ) {
    global $wpdb;
    
    if( isset( $_POST['_term_order'] ) ) {
        $wpdb->update( 
            $wpdb->terms,
            array(
                'term_group' => $_POST['_term_order']
            ),
            array( 
                'term_id'    => $term_id
            )
        );
    }
} // END Function
add_action( 'edited_TAXONOMY_SLUG', 'save_term_order' );

Add Term Order cell to Term List

/**
 * Add Column To Show 'Term Order' Field
 * @param array $columns
 * @return array $columns
 */
function term_order_header( $columns ) {
    $columns['order'] = '<center>Order</center>';
    return $columns;
}
add_filter( 'manage_edit-TAXONOMY_SLUG_columns', 'term_order_header' );

/**
 * Give 'Term Order' Column A Value `term_group`
 * @param string $empty
 * @param string $col_name
 * @param int $term_id
 * @return string
 */
function term_order_header_val( $empty = '', $col_name, $term_id ) {
    if( isset( $_GET['taxonomy'] ) && 'order' == $col_name ) {
        $term = get_term( $term_id, $_GET['taxonomy'] );
        return "<center>{$term->term_group}</center>";
    } else {
        return '0';
    }
}
add_filter( 'manage_TAXONOMY_SLUG_custom_column', 'term_order_header_val', 10, 3 );

Hook Documentation


Below is an out-dated solution that works better if you have multiple Term Meta Values you would like to save. If you only need to save Term Order, the above solution is best.

added 26 characters in body
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175
Loading
Added admin orderby as extra information.
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175
Loading
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175
Loading