Support » Plugins » Hacks » get_posts() no older than X days with comments enabled

  • Resolved jspicher


    Hi guys, l’m trying to create a get_posts() query that will select posts with comments enabled no older than X days. Can anyone help me out w/how this would look, or if it’s even possible?


Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    I don’t think get_posts() knows how to natively query the comment status column, but I could be wrong. Should be able to get at it using the ‘posts_where’ filter. Certainly can get the results using a $wpdb method that accepts a raw SQL query. I can’t easily write SQL queries off the top of my head, so I can’t give you an accurate idea how it would really look. It is possible, so worth figuring out.

    As a really crude suggestion with syntax and naming errors a plenty, something like this could be a direction to follow:

    global $wpdb;
    $posts = $wpdb->get_results("
      SELECT post_ID
      FROM $wpdb->posts
      WHERE comment_status = 'enabled'
      AND DATEDIFF(NOW(), publish_date) < 30");

    Once cleaned up, this should give you an array of post IDs no older than 30 days in which comments are enabled.



    That did the trick, thanks mate.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_posts() no older than X days with comments enabled’ is closed to new replies.