I want to order my categories like I order my pages. I’ve added an extra field called Order to my taxonomy, but how do I order my terms by this value?
Adding and Saving my Order Value:
/** 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 ) {
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];
}
}
//save the option array
update_option( "category_$t_id", strip_tags($cat_meta) );
}
}
add_action ( 'edited_protax', 'save_extra_category_fileds');
Normally I could retrieve this value such as:
$categories = get_categories(array('taxonomy' => 'protax'));
foreach($categories as $cat){
$tmp = get_option('category_'.$cat->cat_ID);
echo $tmp['order'];
}
Is it possible to order by this custom value? Is there a better way to manually order my categories?
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 replaceTAXONOMY_SLUG
with your actual taxonomy slug.Add Term Order cell to Term List
Hook Documentation
{$taxonomy}_edit_form_fields
edited_{$taxonomy}
manage_{$screen->id}_columns
manage_{$this->screen->taxonomy}_custom_column
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.
Calling it:
Edit – You’re also able to order them in the admin panel by using this filter:
What about this: