How to Order Posts by Number of Comments in WordPress

The blog homepage on your WordPress website by default presents all published posts (not pages) in reverse chronological order, with the most recent post appearing at the top. That is for the convenience of both returning visitors who come to read your most recent posts and new visitors who are unlikely to be happy about having to go through several steps to see your most recent posts.

But if you don't like that above "sort posts by published date" behavior and need to "sort posts by number of comments", then select a code given below according to your need and add it into your theme's "functions.php" file.

Code 1:

Sort Homepage Posts by Number of Comments:

function themesdna_sort_homepage_posts_by_number_of_comments( $query ) {
    if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
        $query->set( 'orderby', 'comment_count' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'themesdna_sort_homepage_posts_by_number_of_comments' );

Code 2:

Sort Homepage, Archive Pages (Category Pages, Tag Pages,...), Search Pages Posts by Number of Comments:

function themesdna_sort_posts_by_number_of_comments( $query ) {
    if( $query->is_main_query() && ! is_admin() && ( $query->is_home() || $query->is_search() || $query->is_archive() ) ) {
        $query->set( 'orderby', 'comment_count' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'themesdna_sort_posts_by_number_of_comments' );

Note: If you don't like to edit the functions.php file of your theme, you can use a plugin like "Code Snippets" to run custom code snippets on your site.

Our WordPress Themes

Below are some of our premium WordPress themes. View all our free and premium WordPress themes →
FineWP PRO WordPress Theme

FineWP PRO

If you like free version of FineWP WordPress Theme, you will love the premium version. FineWP PRO is an improved...

$25.00
GreatWP PRO WordPress Theme

GreatWP PRO

If you like GreatWP free WordPress Theme, you will love the premium version. GreatWP PRO is very user friendly, feature-rich,...

$25.00
HotWP PRO WordPress Theme

HotWP PRO

If you like HotWP free WordPress Theme, you will love the HotWP premium version. HotWP PRO is easy to use,...

$25.00
BestWP PRO WordPress Theme

BestWP PRO

If you like BestWP free WordPress Theme, you will love the BestWP premium version. BestWP PRO is user friendly, feature-rich,...

$25.00
GridMini PRO WordPress Theme

GridMini PRO

Do you like the free version of the GridMini WordPress theme? But what if you need more features and options?...

$25.00
ElegantWP PRO WordPress Theme

ElegantWP PRO

If you like ElegantWP free WordPress Theme, you will love the premium version. ElegantWP PRO is very user friendly, feature-rich,...

$25.00
ListMode PRO WordPress Theme

ListMode PRO

$25.00
EasyWP PRO WordPress Theme

EasyWP PRO

If you like EasyWP free WordPress Theme, you will love the premium version. EasyWP PRO is an user friendly, HTML5/CSS3...

$25.00

Can't you choose a single theme? Purchase All Themes for $75.

Save money with our low, one-time price for access to all of our 55 WordPress themes.