Tag Array Sorting issue

10/25/2012 – Still Not solved! Please see below:

My client has a WordPress Tag Cloud (tag array) with tags which include [“] character as well as [The] prefix for some tags. I.e:

Read More
"rose"
"autumn"
The Abby
The Cloud
The Elephant

Obviously all the tags enclosed in the quotations marks [“] are sorted on the top of the list and all the words starting with [The] prefix are sorted somewhere around the letter [T] (following the logical ASC order).

It was sprinkled on me that: “All tags (in the WP tag cloud) have to be ordered Ascending but those which contain the [” “] or [The] characters have to be sorted with all other tags in the chronological order, ignoring the [“] and [The] prefix.

I looked into the WP core function:

**function wp_generate_tag_cloud**

but I have no idea where to start. In the raw SQL statement, I could probably use the trim() to filter out the [” “] and [The] characters form the tag cloud array but that is only a thought which I have no idea how to apply.

Related posts

Leave a Reply

3 comments

  1. wp_generate_tag_cloud() invokes a filter named tag_cloud_sort, which can override the sort order specified in the $args parameter. The tag_cloud_sort filter receives an array of tags and the actual $args parameter passed to wp_generate_tag_cloud(), so it can inspect the full settings of the wp_generate_tag_cloud() invocation and adjust its behavior accordingly.

    You could try something like this:

    function custom_tag_sort($tags, $args) {
        if ($args['orderby'] != 'name') {
            // do not reorder if sort order is not by name.
            // wp_generate_tag_cloud() is smart enough to notice order
            // is not changed and will proceed with its regular sort logic.
            return $tags;
        }
        uasort($tags, 'custom_tag_sort_compare');
    }
    
    function custom_tag_sort_compare($a, $b) {
        return strnatcasecmp(
            custom_tag_sort_normalize($a->name),
            custom_tag_sort_normalize($b->name)
        );
    }
    
    function custom_tag_sort_normalize($tag) {
        // strip quote marks
        $tag = trim($tag, '"');
        // strip leading definitive article
        $tag = preg_replace('/^s*thes+/i', '', $tag);
        return $tag;
    }
    
    add_filter('tag_cloud_sort', 'custom_tag_sort');
    

    Disclaimer: I’ve written this after only a cursory inspection of the wp_generate_tag_cloud() function. I haven’t tested it on a live WordPress installation; I have only verified that the sorting function works correctly on your sample tag cloud:

    The Abby
    "autumn"
    The Cloud
    The Elephant
    "rose"
    
  2. ok so you want to avoid modifying the core code of wordpress… when your client hits the update button after you told him not to, then your going to have to go in and mess with it again.. use action hooks instead. there is conveniently one for hooking into the output for the tag cloud function. add this to your themes functions file

    function tagCloudFilter($tagCloudString, $args)
    {
        $tagCloudString = str_replace('The','', $tagCloudString);
        $tagCloudString = str_replace('"','', $tagCloudString);
    }
    
    add_filter('wp_tag_cloud', 'tagCloudFilter', 10, 2);
    

    That will atleast get rid of the stuff you dont want. as far as sorting it im not to sure but this should get you on your way. it may be easier to sort it with jquery

    If you really want to modify the core code run a foreach loop in the tag array before its formatted and use the str_replaces from above in that loop.. The just run sort() on that array and you should be good. But if it were me i would go with the half solution and not have it alphabetized than to modify the wordpress core

  3. Here’s a thought:

    you could copy the original tag_cloud function and create your own on your functions.php.

    You make the changes you want to make and add this filter inside the function:

    $return = apply_filters( 'YOUR_tag_cloud_function', $return, $args );
    

    And then create the previous filter to add your function to the hook:

    add_filter('wp_tag_cloud', 'YOUR_tag_cloud_function');
    

    I don’t know if it works, I didn’t test it. What do you think?