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 →
GridBit PRO WordPress Theme

GridBit PRO

Are you looking for a feature-rich version of the GridBit Free WordPress Theme? Then you will fall in love with...

$25.00
CuteMag PRO WordPress Theme

CuteMag PRO

CuteMag PRO is an more improved and advanced version of CuteMag Free WordPress Theme. When compare with the CuteMag free...

$25.00
MagSoul PRO WordPress Theme

MagSoul PRO

$25.00
NeatMag PRO WordPress Theme

NeatMag PRO

Do you like the NeatMag Free WordPress Theme?, Then you will love the premium version. NeatMag PRO is very user...

$25.00
GridView PRO WordPress Theme

GridView PRO

GridView PRO is fully responsive, feature-rich, accessibility improved, more advanced version of its free version. GridView PRO WordPress theme has...

$25.00
GridRead PRO WordPress Theme

GridRead PRO

If you like GridRead Free WordPress Theme, then you will love the premium version. GridRead PRO version has color options,...

$25.00
GridPal PRO WordPress Theme

GridPal PRO

If you like the GridPal Free WordPress Theme, then you will love the premium version. GridPal PRO version has color...

$25.00
EliteWP PRO WordPress Theme

EliteWP PRO

If you like EliteWP free WordPress Theme, you will love the premium version. EliteWP PRO is an advanced, very user...

$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.