Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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? Please, help me...

share|improve this question
    
@s_ha_dum, why did you remove your answer? I thought we were close. –  user1627363 Aug 1 '13 at 20:38

2 Answers 2

you can get comment link by wp function

$args = array(
    'number' => '1',
    'post_id' => your_post_id, // use post_id, not post_ID
);
$comments = get_comments($args);

    <a href="<?php echo get_comment_link( $comments->comment_ID ); ?>">postname</a>
share|improve this answer
    
Oeh, almost there... It does this: http://sitename/post-name/comment-page-/#comment- , so it ends with the stripe, no number or whatsoever.... And btw: how do I get it for TODAY's posts only? –  user1627363 Aug 1 '13 at 19:23
    
no option to filter by date, you need to check it with $comments->comment_date or $comments->comment_date_gmt –  Vicky Aug 1 '13 at 19:28
    
But why is your code not working? I can't get a number? –  user1627363 Aug 1 '13 at 19:48

I would use WP_Comment_Query.

function comment_where_wpse_108654($clauses) {
  $clauses['where'] .= ' AND DATE(comment_date) = "'.date('Y-m-d').'"';
  remove_filter('comments_clauses','comment_where_wpse_108654');
  return $clauses;
}
add_filter('comments_clauses','comment_where_wpse_108654');

$args = array(
  'post_id' => $post->ID,
  'number' => 1,
  'orderby' => 'comment_date',
  'order' => 'desc'
);
$comment_qry = new WP_Comment_Query;
$comment = $comment_qry->query( $args );

That assumes that $post->ID is correct.

Now if the theme is building comment IDs right then echo get_permalink().'/#comment-'.$comment[0]->comment_ID; should be a link to your comment.

Edit: You seem to be confused how to make this work with multiple posts/comments.

function comment_where_wpse_108654($clauses) {
  $clauses['where'] .= ' AND DATE(comment_date) = "'.date('Y-m-d').'"';
  return $clauses;
}
add_filter('comments_clauses','comment_where_wpse_108654');

foreach ($results as $id) {

  $args = array(
    'post_id' => $id->ID, // your $id->ID should work just as well
    'number' => 1,
    'orderby' => 'comment_date',
    'order' => 'desc'
  );

  $comment_qry = new WP_Comment_Query;
  $comment = $comment_qry->query( $args );

  if (!empty($comment[0])) { ?>
    <li><a href="<?php echo get_permalink().'/#comment-'.$comment[0]->comment_ID; ?>" rel="bookmark"><?php the_title(); ?></a></li><?php
  }
}
remove_filter('comments_clauses','comment_where_wpse_108654'); 
share|improve this answer
    
So the only thing I have to do is use <?php echo comment_where_wpse_108564(); ?> ? I will test it, thanks. You will here from me. –  user1627363 Aug 1 '13 at 20:14
    
No. The filter is for the query that follows. var_dump($comment) and you will see an array of comment objects-- just one in this case. You'd then echo whatever parts you want. For example, echo $comment[0]->comment_content; –  s_ha_dum Aug 1 '13 at 20:22
    
I don't get it brother. I have pasted your code in to functions.php, I guess that's fine. Than I echoed the thing I said, that showed me the text array indeed, but now, I don't know what to do? Don't I just have to put an anchor and than the href with your echo $comment[0]->comment_content;? I don't get it. I mean it has to do something with the anchor I already have in my first post? –  user1627363 Aug 1 '13 at 20:25
    
FYI: echo $comment[0]->comment_content; gives me just 1 comment. I want per post to get it a link... That's why I am asking you: don't I have to put this in the foreach in the anchor or list? –  user1627363 Aug 1 '13 at 20:31
    
Almost, almost! It is getting the comment of the first-post indeed, which is very very nice, but it does that for the MOST RECENT POST and not per post.... Is there a way that we can make the `comment[0] more generic, so that it does per post's new comment? –  user1627363 Aug 2 '13 at 7:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.