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

Question

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.

Need help.

0
robert0 4 weeks 2021-03-20T19:53:58-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse