wordpress – retrieve content of post and comment in functions.php

I have the method below in functions.php. I’m trying to get at the title of the post that the comment is being left on, and also the name, email, and content of the comment itself.

add_action('comment_post', 'comment_posted');

function comment_posted($comment_id) {
    //what can I do here to get the original title of the post
    //what can I do here to get the details of the comment (name, email, content)?
}

I’ve tried variations of the_title() and get_the_title(), but no luck.

Related posts

Leave a Reply

1 comment

  1. Give this a try:

    add_action('comment_post', 'comment_posted');
    
    function comment_posted($comment_id)
    {
        $comment = get_comment($comment_id);
        $post = get_post($comment->comment_post_ID);
        $title = $post->post_title;
    }
    

    By using get_comment, you’ll have access to all of this information about the comment: http://codex.wordpress.org/Function_Reference/get_comment#Return. Additionally, when you use get_post, you’ll have access to all of this information: http://codex.wordpress.org/Function_Reference/get_post#Return.

    Alternatively, you could simply use:

    $comment = get_comment($comment_id);
    $title = get_the_title($comment->comment_post_ID);
    

    but I prefer to use the get_post function because whenever I need one piece of information from the post, I seem to eventually need another piece.

    Hope this helps!