There is a WordPress hook comment_post to retrieve recently added comment and to pass comment id to get_comment you will get the post id.
add_action( 'comment_post', 'show_message_function', 10, 2 ); function show_message_function( $comment_ID, $comment_approved ) { $comment_array = get_comment($comment_ID ); $comment_post_id = $comment_array->comment_post_ID; }