Most commented posts by time period (last 12h, last 24h and etc)

I have this code which works great.

 <?php 
      $result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 25"); // NUMBER OF POSTS
         foreach ($result as $topten) {
             $postid = $topten->ID;
             $title = $topten->post_title;
             $commentcount = $topten->comment_count;
             if ($commentcount != 0) {
      ?>
        <a href="<?php echo get_permalink($postid); ?>"><span class="tags"> <?php echo $title ?></span></a>
   
   <?php } } ?>

Now it displays (order) the most popular posts by comment count (ALL-TIME).

I know that WordPress understands command ‘period’:

   period=1hourago

Basically, I’m trying to get the above code working with the period, so that I could define:

24hoursago

or

12hoursago

and so on…

I know I need to somehow integrate this into code:

 'date_query' => [
        [
           
        ]
    ],

But I can’t seem to find a way of putting all together.

I saw the idea from: https://www.bitochat.com

Need help.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

So if you want to get the posts that were commented within the last xx hours, e.g. 12 hours, then try one of the following:

Note though, I’m selecting only published posts (post_status = 'publish') and of the post type (post_type = 'post').

And just replace the value of $hours with your preferred number of hours.

  • Using TIMESTAMPDIFF().
    $hours = 12;
    
    $result = $wpdb->get_results( $wpdb->prepare( "
        SELECT p.comment_count, p.ID, p.post_title
            FROM $wpdb->posts p
            INNER JOIN $wpdb->comments c ON c.comment_post_ID = p.ID
        WHERE p.post_type = 'post'
            AND p.post_status = 'publish'
            AND ( TIMESTAMPDIFF( HOUR, c.comment_date, NOW() ) <= %d )
        GROUP BY p.ID
        ORDER BY p.comment_count DESC
        LIMIT 0, 25
    ", $hours ) );
    
  • Or calculate the date and time of xx hours ago and use comment_date >= <date-time xx hours ago>.
    $hours = 12;
    
    $timestamp = current_time( 'timestamp' );
    $date_ago = date( 'Y-m-d H:i:s', $timestamp - $hours * HOUR_IN_SECONDS );
    
    $result = $wpdb->get_results( $wpdb->prepare( "
        SELECT p.comment_count, p.ID, p.post_title
            FROM $wpdb->posts p
            INNER JOIN $wpdb->comments c ON c.comment_post_ID = p.ID
        WHERE p.post_type = 'post'
            AND p.post_status = 'publish'
            AND c.comment_date >= %s
        GROUP BY p.ID
        ORDER BY p.comment_count DESC
        LIMIT 0, 25
    ", $date_ago ) );
    


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x