How to fetch posts that are commented on last 24 hours only in wordpress?

I am trying to fetch all the posts that are commented on last 24 hours range.
What I have tried is:

global $wpdb;
$results = $wpdb->get_results( "SELECT comment_post_ID FROM wp_comments WHERE comment_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'", OBJECT );

But from above query I am getting all the posts whose comment date is greater than todays datetime. I want to fetch post which are commented under 24 hours.

Read More

Can anybody help me in this. Any help would be appreciated..:)

Thanks in advance.

Related posts

1 comment

  1. Take this . First one will give current datetime and second one will give 24 hours back time that is past day with the same time

    echo $date = date("Y-m-d H:m:s");
    echo '<br>';
    echo $date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
    

    so you query should be like :

    global $wpdb;
    $results = $wpdb->get_results( "SELECT comment_post_ID FROM wp_comments WHERE comment_date > '" . date("Y-m-d H:m:s", strtotime('-24 hours', time())) . "'", OBJECT );
    

Comments are closed.