0

I have found the following function to change the behavour of the Wordpress tag cloud:

function widget_custom_tag_cloud($args) {

    $args['orderby'] = 'count';
    return $args;
}
add_filter( 'widget_tag_cloud_args', 'widget_custom_tag_cloud' );

However, I need to change the order from 'count' to 'menu_order'. Changing this line:

$args['orderby'] = 'count';

to

$args['orderby'] = 'menu_order';

does NOT work.

Is it possible to do this or do I need to write a custom widget from scratch?

Thanks in advance for any help!

  • 1
    what to you mean by menu_order here? – birgire Nov 29 '16 at 11:13
2

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).

0

If you mean term_order, then you could use the tag_cloud_sort filter to handle the sorting via PHP.

Here's an example, using usort:

add_filter( 'tag_cloud_sort', function( $tags, $args )
{
    // Nothing to do if no tags
    if( empty( $tags ) || ! is_array( $tags ) )
        return $tags;

    // Custom tag sort
    uasort( $tags, function( $a, $b )
    {
        if( $a->term_order === $b->term_order )
            return 0;

        return $a->term_order < $b->term_order ? - 1 : 1; // ASC
    } );

    return $tags;
}, 10, 2 );

that can be simplified in PHP 7 by using the spaceship comparison operator:

add_filter( 'tag_cloud_sort', function( $tags, $args )
{
    // Nothing to do if no tags
    if( empty( $tags ) || ! is_array( $tags ) )
        return $tags;

    // Custom tag sort
    usort( $tags, function( $a, $b )
    {
        return $a->term_order <=> $b->term_order; // ASC (swap $a and $b for DESC)
    } );

    return $tags;
}, 10, 2 );

The wp_generate_tag_cloud() function uses uasort(), but I don't think we need to preserve the index association here.

PS:

The wp_cloud_tag() function uses get_terms(), so an alternative could be to support term_order ordering in get_terms() via the get_terms_orderby filter. See e.g. a recent answer here. I just tested the term_order ordering by using the plugin mentioned by OP in the question that was answered there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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