• I am creating a WordPress page for a semi full list of our (many, many) tags. Along with the list, I would like to create a “most popular tags” section, where the top tags are sorted by their post counts, and limited to say the top 25.
    Is this possible? Using count_order only orders within a letter (ie, most popular tags starting with A, most popular tags starting with B, etc).
    I would like to order tags by count while using the basic=”yes” option, showing a list of most popular tags by count.

Viewing 1 replies (of 1 total)
  • Plugin Author tugbucket

    (@tugbucket)

    count_order works as designed. The plugin separates all results alphabetically and groups them by their respective letter. The basic option doesn’t change this. They are still grouped by letter.

    My plugin isn’t designed to do what your are wanting.

    The below code will return a list of your 25 most used (by tag count) tags and sort them alphabetically within each count.

    for example:
    Potato (3)
    Apples (2)
    Bananas (2)
    Celery (1)
    Raisin (1)
    Spinach (1)
    etc…

    <?php
    	$tags = get_terms(array(
    		'taxonomy' => 'post_tag',
    		'orderby' => 'count',
    		'order' => 'DESC',
    		'number' => 25
    	));
    	$groups = array();
    	foreach ($tags as $key => $item) {
    		$groups[$item->count][$key] = $item;
    
    	}
    	sort($groups, SORT_NUMERIC);
    	$grouped = array();
    	foreach($groups as $group){
    		usort($group, function ($a, $b) {
    			return $a->slug <=> $b->slug;
    		});
    		$grouped[] = $group;
    	}
    	$groups = array();
    	foreach($grouped as $g){
    			$groups = array_merge($groups, $g);
    	}
    	echo '<ul>'."\n";
    	foreach($groups as $tag){
    		echo '<li><a href="'.get_tag_link($tag->term_id).'">'.$tag->name.' ('.$tag->count.')</a></li>'."\n";
    	}
    	echo '</ul>'."\n";
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Sort by count with “basic”’ is closed to new replies.