Enable the admin to sort custom taxonomy

I added a custom taxonomy (separate categories for a custom post type) like this:

register_taxonomy(self::$slug . '_category', array(self::$slug), array(
    'label' => 'Kategorien',
    'labels' => array(
        'name' => 'Kategorien',
        'singular_name' => 'Kategorie',
        'all_items' => 'Alle Kategorien',
        'edit_item' => 'Kategorie bearbeiten',
        'view_item' => 'Kategorie anzeigen',
        'update_item' => 'Kategorie aktualisieren',
        'add_new_item' => 'Neue Kategorie erstellen'
    ),
    'hierarchical' => true,
    'rewrite' => array(
        'slug' => 'referenzen-kategorie'
    )
));

On the homepage I am fetching the categories like this:

Read More
$categories = get_categories(array(
    'type' => 'sb_reference',
    'taxonomy' => 'sb_reference_category'
));

And displaying it. Now comes the catch: The admin of the site wants to influence the order of the displayed categories and his desired order is neither alphabetical nor by post count, but by his perceived relevance of the categories. So basically I have to give the taxonomy an additional field that he can write the order of the categories in (i.e. CategoryA is 0, CategoryB is 2, CategoryC is 1 etc.)

My question: How can I add an additional field to the custom taxonomy or what are my alternatives?

Related posts

Leave a Reply

1 comment

  1. Thanks to this great tutorial, I just found the answer myself.

    WordPress offers the action hooks sb_reference_category_add_form_fields, sb_reference_category_edit_form_fields, create_sb_reference_category and edited_sb_reference_category (these are named after the custom taxonomy, in my case sb_reference_category). With these hooks, it is easy as pie to add and save a custom field to the taxonomy.