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