[resolved] List tags and sort by number of posts with that tag (3 posts)

  1. prtscreen
    Posted 5 years ago #

    Hi all!
    I've been strugling with this for a while now, but cant figure it out. Guess its more of a php question then wp..

    Ok. I want to List all my tags and i want to order them by the ammount of posts that has the tag.
    This is what i got so far:

    $tags = get_tags();
    			$tags_count = count($tags);
    			$top = 15;
    			for ($i = 0;$i < $tags_count;$i++):
    			$slug = $tags[$i]->slug;
    			if ($i < $top){
    			echo "<li><a href='".get_tag_link($tags[$i]->term_id)."'>".$slug."</a></li>";

    The reason for $top is that after 15 tags listed im hiding the others, displaying them when user want to view more.

    So, could anyone point me to how to resolve this?! Would be awesome. I know how to count the posts for each tag. But not ho to sort by it!

  2. Mark / t31os
    Posted 5 years ago #

    Use get_terms and set the orderby to count, and order to desc.

    No need to do any counting then, it's all done for you.. ;)

  3. prtscreen
    Posted 5 years ago #

    Amazing! Thanks a bunch, every day wordpress surprise me ;)

Topic Closed

This topic has been closed to new replies.

About this Topic