WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can you help me with something simple?

How can I get the post id of the last approved comment? I want to assign it to a variable.

Thanks so much in advance!

share|improve this question
$recent_comments = get_comments( array(
    'number'    => 1,
    'status'    => 'approve'
) );
foreach($recent_comments as $comment) :
    $latest_postid_with_comment = $comment->comment_post_ID;

Probably way to compress this code into something smaller. But this will work.

share|improve this answer
Thanks so much Weston. What do I do if there are no approved comments yet? I would like to set it to the first post then. How should I do that? – Marton Feb 27 '12 at 1:25

Your Answer


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.