Get link to (first) comment if there are comments today in WordPress

I have this function and a link to a post:

<?php
foreach ($results as $id) {
  $post = &get_post( $id->ID );
  setup_postdata($post);

  <li><a <?php href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>

</li>
  <?php
} ?>

What I want to do is this: if there is a comment posted today, than show me the link to the first comment that has been posted. So for example: if there are 4 comments made today, I want the link to the first comment instead of the permalink just like now.

Read More

I tried using this:
<a <?php href="<?php the_permalink(get_comments->$post_id) ?>">postname</a> and variations of this like comment_post_ID, but I could not get it to work. What am I doing wrong and what should I do?

Related posts

Leave a Reply

1 comment

  1. I think you may be confused about objects and functions (either that or you copy/pasted in wrong). When you tried to do get_comments->$post_id, that wouldn’t work because get_comments is not an object, get_comments is a function that accepts parameters. Check out what I did below as it may help you:

    <?php
    foreach ($results as $id) {
      $post = &get_post( $id->ID );
      setup_postdata($post);
    
      $args = array('post_id' => $id->ID, 'number' => 1);
      $lastComment = get_comments($args);
    
      if (!empty($lastComment[0]) and $lastComment[0]->comment_date > date('Y-m-d 00:00:00')){
        echo '<li><a href="'.get_comment_link($lastComment[0]).'">'.the_title().'</a></li>';
      }
    ?>
    

    get_comments() will take a bunch of arguements, in the above I passed it the postID and the count of 1, so that it only pulls the last comment. It will still pull comments not made today though so the if condition I added in should only echo it if the comment was posted after midnight on today.