How to Display Last Month’s Most Commented Posts as Thumbnails in WordPress

Home » Snippets » How to Display Last Month’s Most Commented Posts as Thumbnails in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

If you run a blog, you probably know how important it is to keep your readers engaged and coming back for more. One way to do this is by displaying a list of your most commented posts. But what if you want to add some visual appeal to this list? In this article, we’ll show you how to display the most commented posts from the previous month as thumbnails, using some simple code and a little bit of customization. This will help your readers quickly identify and click on the most engaging content on your site from the past month.

					function wpturbo_last_month_most_commented_posts() {
    $last_month_start = date('Y-m-d', strtotime('first day of last month'));
    $last_month_end = date('Y-m-d', strtotime('last day of last month'));
    $args = array(
        'post_type'=> 'post',
        'date_query' => array(
            'after' => $last_month_start,
            'before' => $last_month_end,
            'inclusive' => true
        'orderby' => 'comment_count',
        'order' => 'DESC',
        'posts_per_page' => 5
    $query = new WP_Query( $args );
    if ($query->have_posts()) { ?>
        <h2><?php _e('Last month most commented posts', 'wpturbo'); ?></h2>
        <div class="wpturbo-commented-posts-container">
        <?php while ($query->have_posts()) {
            $query->the_post(); ?>
            <div class="wpturbo-commented-post">
                <a href="<?php the_permalink(); ?>"><img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" alt="<?php the_title_attribute(); ?>"></a>
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <p><?php the_time('F jS, Y'); ?></p>
        <?php }
        wp_reset_postdata(); ?>
    <?php }
add_action( 'wp_footer', 'wpturbo_last_month_most_commented_posts' );

The code snippet above displays the last month's most commented posts as thumbnails.

First, we define $last_month_start and $last_month_end variables using the PHP’s date() function. These variables specify the start and end dates of the previous month.

Then, we define $args array which is used to initiate the WordPress query. We set various parameters to query five most commented posts of the previous month.

Next, we initiate a new instance of WP_Query by passing $args as a parameter. After that, we check if there are any posts returned by the query using the $query->have_posts() function. If there are, we display the most commented posts of the last month by iterating over them with while ($query->have_posts()).

For each post, we first create the HTML structure to display its thumbnail, title, and date. Then, we use the WordPress function get_the_post_thumbnail_url() to retrieve the image URL for the thumbnail of the current post. We output this URL to the src attribute of the <img> tag.

Finally, we add the retrieved posts to a container with the class wpturbo-commented-posts-container and display them on the frontend using the add_action() function and hooking into the wp_footer action.

Register an account to save your snippets or go Pro to get more features.