Title: Retrieving Post ID For Comments?
Last modified: August 19, 2016

---

# Retrieving Post ID For Comments?

 *  Resolved [spewf](https://wordpress.org/support/users/spewf/)
 * (@spewf)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/)
 * 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;
       ?>
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [Rahul Sonar](https://wordpress.org/support/users/rahulsonar/)
 * (@rahulsonar)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599744)
 * you can write a custom query:
 * `SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = '$comm->ID'`
 *  Thread Starter [spewf](https://wordpress.org/support/users/spewf/)
 * (@spewf)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599972)
 * Would I do this instead of the $comments = get_comments ?
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599973)
 * 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 …
 *  Thread Starter [spewf](https://wordpress.org/support/users/spewf/)
 * (@spewf)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599974)
 * The code works but the image will not. I want it to show the custom field that
   is attached to the post.
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599975)
 * (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 ^^
 *  Thread Starter [spewf](https://wordpress.org/support/users/spewf/)
 * (@spewf)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599977)
 *     ```
       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.
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599979)
 * 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](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 * 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
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599981)
 * 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 ...
       ```
   
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1599982)
 * 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);
       ```
   
 *  Thread Starter [spewf](https://wordpress.org/support/users/spewf/)
 * (@spewf)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1600025)
 * 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;
       ?>
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Retrieving Post ID For Comments?’ is closed to new replies.

## Tags

 * [$post->id](https://wordpress.org/support/topic-tag/post-id/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 4 participants
 * Last reply from: [spewf](https://wordpress.org/support/users/spewf/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/retrieving-post-id-for-comments/#post-1600025)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
