Change order of WordPress tag cloud

Tags (terms) do not have a menu_order (see the design of the table in the DB).

If you want to give terms a ‘menu_order’ you will need to create this yourself.

As long as your WP is >= 4.4.0, you can make use of the feature term_meta.

This is to terms what post meta is to posts.

You can create a ‘menu_order’ ‘custom field’ for terms and then you can set the menu order when creating/editing a term.

The relevant functions are:

add_term_meta();

update_term_meta();

get_term_meta();

delete_term_meta();

See here – https://codex.wordpress.org/Function_Reference/add_term_meta

And when query, your code won’t do the trick for term meta. You need to write your own widget, that contains get_terms(). E.g.

$args = array(
    'taxonomy'   => 'taxonomy_name', //can be array with multiple tax
    'meta_key' => 'menu_order',
    'orderby' => 'meta_value',
    'order' => 'DESC',
);

$terms = get_terms($args);

To build the UI in admin panel & saving functions for adding/editing term’s meta, the proccess is a little long for a SO/SE answer, I think.

If you Google ‘wp term meta’ you’ll find out how to do it.

You will need 4 or 5 functions in all.

The hooks you will use are:

{$taxonomy}_add_form_fields // add the custom field to the 'new term' form

{$taxonomy}_edit_form_fields // add the custom field to the 'edit term' form

create_{$taxonomy} // for saving the term meta from the 'new term' form

edit_{$taxonomy} // for saving the term meta from the 'edit term' form

manage_edit-{$taxonomy}_columns // OPTIONAL adds a column, for the custom field, in the terms table for the taxonomy

Or, use a plugin like this one (or copy the code in it).

Leave a Comment