Show Top 10 Posts in WordPress Without a Plugin

Wordpress is a great blogging tool and many plugins are available to do just about everything. But having 100 plugins will make your site crawl and will annoy your users.

This tutorial will show you how to add some simple code to show the top ten posts for various metrics anywhere on your blog

Instructions

Top Posts By Comments

The following code gets posts from the database and orders them by the amount of comments they have and selects the top 10.

If you want to change the amount of posts displayed change LIMIT 0 , 10 to another number (0 is the first post and 10 is the last)

If you wish to choose the least posted comments then change DESC to ASC

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 7");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>	
<a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?>
<?php } }?>

The last but one line echos the title of the post as a link. To use this code just copy and paste it where you wish the code to appear

Most Recent Posts

an oldy but a goody. The code to show the ten most recent posts that have been posted on your blog

Just change showposts=10 to another number to change the amount of posts shown

The last but one line echos the title of the post as a link. To use this code just copy and paste it where you wish the code to appear

<?php query_posts('showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent link to'); ?>">
<?php the_title(); ?></a>
<?php endwhile;?>

Most Viewed Posts

I decided to include this one even though there is no code for it. WordPress does not save any information on how many pages are viewed therefore it is impossible to show this information without using a plugin or some 3rd party monitoring software

If you have found a way using only code please let me know!

Let me know if this helped you! Leave a comment.

Leave a Reply