WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieving Post ID For Comments? (11 posts)

  1. spewf
    Member
    Posted 3 years ago #

    I am trying to have a custom field showed on my recent comment code. Any ideas?

    <?php
    
    $image = get_post_meta($post->ID, 'photo', true);
    
    $comments = get_comments('number=30');
    
      foreach($comments as $comm) :
    
      $url = '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    <li>
    <?php echo get_avatar($comm->comment_author_email, 30); ?>
    <strong><?php echo $url; ?></strong>
    <p><?php echo $comm->comment_content; ?></p>
    
    <img src='<?php echo $image; ?>' height='100' width='180' style='border:3px double white;' />
    </li>
    <?php
      endforeach;
    ?>
  2. Rahul Sonar
    Member
    Posted 3 years ago #

    you can write a custom query:

    SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = '$comm->ID'

  3. spewf
    Member
    Posted 3 years ago #

    Would I do this instead of the $comments = get_comments ?

  4. forunner
    Member
    Posted 3 years ago #

    I don't understand =p
    the code that you gave in the first post works fine no?
    but it will always display the same image linked by the meta ...

  5. spewf
    Member
    Posted 3 years ago #

    The code works but the image will not. I want it to show the custom field that is attached to the post.

  6. forunner
    Member
    Posted 3 years ago #

    (I think ) Rahul Sonar was suggesting you to make an sql request to find the ID of the post, and, after, to use this ID to call your meta ^^

  7. spewf
    Member
    Posted 3 years ago #

    sql = "SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = '$comm->ID'";
    $image = get_post_meta($post->ID, 'photo', true);
    
    $comments = get_comments('number=30');

    Causes errors. Hmm, I am not seeing something here.

  8. forunner
    Member
    Posted 3 years ago #

    I don't think you can do sql request like that lol [maybe it works in wordpress, but this is the first time I see that thing]
    I'm using pdo to do my request, but try to look here : wpdb

    try to use the ID that you will find in your call of the photo instead $post->id

    I may be totally wrong about the solution... maybe Rahul Sonar was thinking about something else

  9. keesiemeijer
    moderator
    Posted 3 years ago #

    Ive tried this somewhere once

    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, comment_content
    		FROM $wpdb->comments
    		LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
    		WHERE (post_status = 'publish' OR post_status = 'static') AND comment_approved= '1'
    		ORDER BY comment_date_gmt DESC
    		LIMIT 30";
    $comments = $wpdb->get_results($sql);

    you can use this in stead of $comments = get_comments
    the array $comments is populated with these values wich you can use in your function:

    [ID] => 11 //this is the post ID
    [post_title] => dolor
    [post_password] =>
    [comment_ID] => 12
    [comment_post_ID] => 11
    [comment_author] => admin
    [comment_date_gmt] => 2010-07-23 09:43:39
    [comment_approved] => 1
    [comment_type] =>
    [comment_content] => bla bla bla ...
  10. keesiemeijer
    moderator
    Posted 3 years ago #

    I see now that $comments = get_comments('number=30'); also will give you the post ID [comment_post_ID] => 11
    Put this inside the foreach statement of your function:

    $image = get_post_meta($comm->comment_post_ID, 'photo', true);
  11. spewf
    Member
    Posted 3 years ago #

    Wow keesiemeijer, that worked pefectly. I just did your last idea and it worked. Here is the final code I have:

    <?php
     $comments = get_comments('number=30');
    
     foreach($comments as $comm) :
      $image = get_post_meta($comm->comment_post_ID, 'photo', true);
      $url = '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' . $comm->comment_author . '</a>';
    ?>
    <li>
    <?php echo get_avatar($comm->comment_author_email, 30); ?>
    <strong><?php echo $url; ?></strong>
    <p><?php echo $comm->comment_content; ?></p>
    
    <img src='<?php echo $image; ?>' height='100' width='180' style='border:3px double white;' />
    </li>
    <?php
      endforeach;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags