Feature
Post

Category
Code


WordPress Snippet: Display Posts by Tag Frequency

Blogs just aren’t the same as they used to be. In the old days a blog was characterized by time and personality—chronology was important, and you read the blog because of the author, not the information he could give you.

Nowadays blogs are used more for short bursts of information about a single topic—typically a blogger’s expertise. The personality behind a site barely matters. “Content is king!” as you may recall.

So if chronology is no longer important, why post by date? I think part of the answer is that WordPress displays dates by default in most themes. Let me propose an alternative way of displaying content in WordPress. By tag frequency.

Making tags useful

Tags provide wonderful meta information about a post, but sadly this information is typically relegated to an ugly list in the sidebar. I say, if you’re going to go to the work of tagging all those posts, you had better use those tags for something useful!

By adding a couple snippets outside “The Loop” in WordPress you can display N number of posts by X tag. Here is an example to help make it a little more clear:

Imagine you have 20 posts tagged Home and Garden, 10 tagged Clearance, and 13 tagged Hardware. With a bit of PHP we can get WordPress to display posts from each tag in order of most popular to least popular. We can even take it a step further and limit the number of tags to display. The end result might look something like this:

HOME AND GARDEN
* Tag 1 Post 1 Title
* Tag 1 Post 2 Title
* Tag 1 Post 3 Title
* Tag 1 Post 4 Title

HARDWARE
* Tag 2 Post 1 Title
* Tag 2 Post 2 Title
* Tag 2 Post 3 Title
* Tag 2 Post 4 Title

CLEARANCE
* Tag 3 Post 1 Title
* Tag 3 Post 2 Title
* Tag 3 Post 3 Title
* Tag 3 Post 4 Title

Now that is a usable list of tags that makes the content readily available. A great side effect of displaying posts by tag frequency is that your site become much more dynamic. For example, maybe you go to a plumbing conference and blog 30 posts about it, all tagged Plumbing. The content on the site will automatically start displaying the posts tagged with Plumbing at the top of the list. I call it the “bubbling” homepage.

Enough rambling, here’s the code:

      <?php
      $noOfTags = 10; 
      $noOfPosts = 4;
      
      $cloudRight = get_tags("orderby=count&order=DESC&number=$noOfTags");
      foreach((array)$cloudRight as $tagRight) : ?>
          
          <?php        
          $postsRight = new WP_Query();
          $postsRight->query("tag={$tagRight->slug}&showposts=$noOfPosts");        
          ?>            
          <?php if ( $postsRight->have_posts() ) :?>
            <dl class="xoxo">
              <dt><?php echo $tagRight->name ?></dt>
            
              <?php while ( $postsRight->have_posts() ) : $postsRight->the_post(); ?>
              <dd><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></dd>
              <?php endwhile;?>
            
            </dl>
          <?php endif; ?>            
          
      <?php 
          unset($postsRight);
      endforeach; ?> 

Just paste this bad boy outside The Loop in your index.php file and there you have it, clutter free, bubbling posts by tag frequency.


  1. By Lorraine Nepomuceno posted on March 28, 2009 at 4:04 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Nice, Dustin. I never really liked date-based navigation myself- this makes more sense.

  2. By nhan posted on May 7, 2009 at 3:07 am
    Want an avatar? Get a gravatar! • You can link to this comment

    very useful snippet, thank-you.

  3. By Bruno posted on May 8, 2009 at 3:13 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Hi! :)

    I’m using your code and it works perfectly. Still, i would like to show only one specific tag. How do I do that? I tried the “$slug” string, but it didn’t worked :(

  4. By Ben posted on July 2, 2009 at 9:26 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Very useful thanks. Saved me a lot of time.

  5. By Dennis posted on September 28, 2009 at 12:30 am
    Want an avatar? Get a gravatar! • You can link to this comment

    This is a very useful post. I’ve been trying to figure this out so thank you.

  6. By Randa Clay posted on October 10, 2009 at 6:04 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Thank you so much! I’m not sure if the author is still available to answer this question, but what I want to do is put this on my category template and I want to only display the lists of posts if they are in the category the user has clicked on. How would I modify the code to make this happen?

  7. TrackbackWordpress Snippet Display Posts by Tag Frequency Devlounge | Wood TV StandPrimary Guide for WordPress Code Snippets - Blog Reign