Generating an Automatic Popular Post List via Del.icio.us or Digg in Wordpress

Published July 30, 2008 2 Comments 8 Delicious Bookmarks


Photo Credit: reportergimmi

A few months ago I wrote an article on how to create a popular posts list, and in the example I used a category called ‘Popular’ to determine what was shown within the list. This approach is great for those that remember to periodically update their list by adding new posts to that category, but I wanted a way for this to be done automatically.

Auto Popular List based on Number of Comments

In this example I check the number of comments, and if they are greater than or equal to 10 (see the 8th line of the code below) then I display the post. This means that this Loop will show any post that has 10 or more comments. The $popular->query('showposts=50'); at the top of the script is so that this Loop will run the ‘number of comments’ check for each of your 50 latest posts.

The trick here will be finding the happy medium between those two variables. If you have the ‘number of comments’ variable set to low, you could get too many posts in your list that meet the criteria. But on the other hand, if you have the ’showposts’ variable set too low then there may not be enough posts that end up hitting on the ‘number of comments’. A balance needs to be met here. If it was me, I would shoot to have approximately 3 to 5 posts show up in my list. Of course, since this is a dynamically created, the number of posts shown number can change at any given time.

<h3>Popular Articles</h3>
<ul class="postlist">
<?php
$popular = new WP_Query();
$popular->query('showposts=50');
while ($popular->have_posts()) : $popular->the_post();
    $total = get_comments_number();
    if ($total >= 10) { ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Score: <?php echo $total; ?></span></li>
    <?php }; ?>
<?php endwhile; ?>

Auto Popular List based on Comments & Del.icio.us Bookmarks

This example is exactly the same except for on the $total line I use my own WP-SocialCount plugin to pull in the amount of Del.ico.us bookmarks that the post has. I feel this gives us a more complete idea of whether or not a post is really “popular” — rather than solely relying on it’s number of comments.

<h3>Popular Articles</h3>
<ul class="postlist">
<?php
$popular = new WP_Query();
$popular->query('showposts=50');
while ($popular->have_posts()) : $popular->the_post();
    $total = (get_comments_number() +  wpsocialcount('delicious', 'count', get_permalink()));
    if ($total >= 10) { ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Score: <?php echo $total; ?></span></li>
    <?php }; ?>
<?php endwhile; ?>

Digg too…

If your posts regularly show up on Digg as well, then you can use the same plugin and replace the $total line (line 7) in the above example with this:

$total = (get_comments_number() +  wpsocialcount('delicious', 'count', get_permalink()) +  wpsocialcount('digg', 'diggs', get_permalink()));

Scoring Methods

The default scoring will be all that all ‘hits’ are of equal strength:
Total Score = Comments + Del.icio.us Bookmarks + Diggs

Alternatively, if you feel that a Digg or Bookmark is twice as important as a regular old comment, then you could change the $total line to reflect this.

$total = (get_comments_number() + 2*(wpsocialcount('delicious', 'count', get_permalink()) + wpsocialcount('digg', 'diggs', get_permalink())));

2 Comments

  1. Bright said:

    Thanks! This is really helpful. I’ve been looking around the web for a simple way to show popular posts and your article explained it very well. I was wondering… is there a way to modify the code a bit so that posts are ordered by their “Scores”? That way, posts with the most number of comments are displayed first before the others.

  2. @Bright – You’re welcome! As for the advanced sort – probably, but I would have to re-look at the code in order to find out how to do it. I know there has to be a way to do it, so I’ll post back here if I ever get it working.

Leave your Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get your own Gravatar