WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a WordPress rookie. I've added a custom field to my "Category" taxonomy. The custom field is "custom_order" and its purpose is to hold a number so that my categories can be sorted and displayed in the order I choose.

My problem is that I can't get them to sort; I can display the categories, and 'echo' each category's "custom_order" number, but I must be missing something when it comes to accessing and sorting the meta_value.

Not sure if I'm going about this the right way either.

Here is my code to add the field to the 'New Category' page:

function taxonomy_add_new_meta_field() 
{ ?>
<div class="form-field">
    <label for="term_meta[custom_order]">Order</label>
    <input type="number" name="term_meta[custom_order]" id="term_meta[custom_order]" value="">
    <p class="description">Enter an order number for organizational purposes</p>

add_action( 'category_add_form_fields', 'taxonomy_add_new_meta_field', 10, 2 );

Here is my code to add the field to the 'Edit Category' page:

function taxonomy_edit_meta_field($term) {

$t_id = $term->term_id;

$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[custom_order]">Order</label></th>
        <input type="number" name="term_meta[custom_order]" id="term_meta[custom_order]" value="<?php                 echo esc_attr( $term_meta['custom_order'] ) ? esc_attr(     $term_meta['custom_order'] ) : ''; ?>">
        <p class="description">Enter an order number for organizational purposes</p>
add_action( 'category_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2);

Here is my code to save the contents of the field:


function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $cat_keys = array_keys( $_POST['term_meta'] );
    foreach ( $cat_keys as $key) {
        if ( isset ( $_POST['term_meta'][$key] ) ) {
            $term_meta[$key] = $_POST['term_meta'][$key];
                update_option( "taxonomy_$t_id", $term_meta );

add_action( 'edited_category', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_category', 'save_taxonomy_custom_meta', 10, 2 );

The above snippets of code I got from this website:


Last but not least here is my attempt to sort my categories based on this field:

$args = array(
'parent' => $category->cat_ID,
$subcategories = get_categories($args);

I then iterate through the $subcategories array with a foreach() loop. I thought I had it working until I added a third sub-category; I now see that it puts 'custom_order' 1 first, 'custom_order' 3 second, and 'custom_order' 2 third.

Can anybody lend me a hand?

share|improve this question
you're not going to be able to get the correct order from get_categories, the API knows nothing of your custom meta implementation. you'll have to get all the categories and order them yourself in php. – Milo Apr 1 '14 at 23:59
Ok I figured that may be the case. Thank you Milo! – jon Apr 2 '14 at 1:48

I would edit the SQL database directly, changing category IDs based on my desired order. Then I would use 'orderby'=>'id' to sort.

You could also add an integer to the slug (i.e., 1-fish, 2-chips, 3-dips, etc.) and order by slug, but those would appear on the front end.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.