[resolved] get_posts() no older than X days with comments enabled (3 posts)

  1. jspicher
    Posted 3 years ago #

    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?


  2. bcworkz
    Posted 3 years ago #

    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.

  3. jspicher
    Posted 3 years ago #

    That did the trick, thanks mate.

Topic Closed

This topic has been closed to new replies.

About this Topic