• Hello,

    I want to display the most popular posts by number of comments on my website.
    I’ve installed plugins for these, however it only detects all comments made after the plugin was installed.

    Is there any way/plugin that can fetch the existing data (specifically number of comments) of my posts?

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • you can get posts ordered by ‘comment_count’ – https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
    and create a corresponding query to fetch the posts.

    example code:

    <?php $most_commented = new WP_Query( array( 'post_type' => array( 'post' ), 'posts_per_page' => 3, 'order' => 'DESC', 'orderby' => 'comment_count', 'ignore_sticky_posts' => true ) );
    	if( $most_commented->have_posts() ) : ?>
    		<section class="most-commented-posts">
    			<h5 class="section-title">Most Commented Posts</h5>
    <?php while( $most_commented->have_posts() ) :
    		$most_commented->the_post(); ?>
    			<div class="section-entry">
    				<h6 class="most-commented-post-title"><?php the_title(); ?></h6>
    				Comment count: <?php echo get_comments_number(); ?>
    			</div>
    <?php endwhile; ?>
    		</section>
    <?php endif;
    wp_reset_postdata(); ?>

    html structure and formatting depend on your currently used theme and on the intended location for the output.

    this is working i recently checked on my hand and implement using short code.
    if you want to add short code on any page and you can get all mix commented post

    // function that runs when shortcode is called
    function wpb_demo_shortcode() { 
     
    // Things that you want to do. 
     $most_commented = new WP_Query( array( 'post_type' => array( 'post','product' ), 'posts_per_page' => 2, 'order' => 'DESC', 'orderby' => 'comment_count', 'ignore_sticky_posts' => true ) );
    	if( $most_commented->have_posts() ) : ?>
    		<section class="most-commented-posts">
    			<h5 class="section-title">Most Commented Posts</h5>
    <?php while( $most_commented->have_posts() ) :
    		$most_commented->the_post(); ?>
    			<div class="section-entry">
    				<h6 class="most-commented-post-title"><?php the_title(); ?></h6>
    				Comment count: <?php echo get_comments_number(); ?>
    			</div>
    <?php endwhile; ?>
    		</section>
    <?php endif;
    wp_reset_postdata(); 
     
    // Output needs to be return
    //return $message;
    } 
    // register shortcode
    add_shortcode('shortcode_custom', 'wpb_demo_shortcode'); 
    

    you can customize html as you want and same like your theme . only pas [shortcode_custom] this short code any where and feach all mix commented post and products title,
    for watching video visit on given bellow link.
    click here

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fetch existing numbers of comments’ is closed to new replies.