WordPress 101 Guide | Make a Query to Get the Most Popular Post by Comment Count in WordPress

Wordpress 101 Guide

This one is very simple. For those of you that want to automatically include it in your themes, you can query by comment count using get_posts and running a foreach loop to get the contents. Very simple procedure.

Code below will output the most popular post by comment count.

<? $args = array(‘posts_per_page’ => 5, ‘orderby’ => ‘comment_count’);
global $post;
$popular = get_posts($args);
foreach($popular as $post) {
setup_postdata($post); ?>

<a href="<?php the_permalink(); ?>"><?php the_title();?></a>

<?php endforeach;?>

Give it a try on your template.

Leave a Comment