Skip to main content
Added docblock and error checking. props @christopher-ross
Source Link
Morgan Estes
  • 1.5k
  • 12
  • 22

The Codex makes it seem like you can query for a specific comment by ID, as does the GenerateWP query generator, but I couldn't get it to work with either of those examples. Even looking through the WP_Comment_Query:query() code makes it clear that you should be able to pass the ID in the parameters.

That said, using get_comment() is your only way to go for now. Here's what can work based on your original code:

<?php
/**
 * Get the contents of a single comment by its ID.
 * 
 * @param  int $comment_id The ID of the comment to retrieve.
 * 
 * @return string The comment as a string, if present; null if no comment exists.
 */
function wpse120039_get_comment_by_id( $comment_id ) {
    $comment = get_comment( intval( $comment_id ) );

    if ( ! empty( $comment ) ) {
        return $comment->comment_content;
    } else {
        return '';
    }
}

echo '<p>' . wpse120039_get_comment_by_id( '34' ) . '</p>';

The Codex makes it seem like you can query for a specific comment by ID, as does the GenerateWP query generator, but I couldn't get it to work with either of those examples. Even looking through the WP_Comment_Query:query() code makes it clear that you should be able to pass the ID in the parameters.

That said, using get_comment() is your only way to go for now. Here's what can work based on your original code:

<?php
function wpse120039_get_comment_by_id( $comment_id ) {
    $comment = get_comment( $comment_id );

    return $comment->comment_content;
}

echo '<p>' . wpse120039_get_comment_by_id( '34' ) . '</p>';

The Codex makes it seem like you can query for a specific comment by ID, as does the GenerateWP query generator, but I couldn't get it to work with either of those examples. Even looking through the WP_Comment_Query:query() code makes it clear that you should be able to pass the ID in the parameters.

That said, using get_comment() is your only way to go for now. Here's what can work based on your original code:

<?php
/**
 * Get the contents of a single comment by its ID.
 * 
 * @param  int $comment_id The ID of the comment to retrieve.
 * 
 * @return string The comment as a string, if present; null if no comment exists.
 */
function wpse120039_get_comment_by_id( $comment_id ) {
    $comment = get_comment( intval( $comment_id ) );

    if ( ! empty( $comment ) ) {
        return $comment->comment_content;
    } else {
        return '';
    }
}

echo '<p>' . wpse120039_get_comment_by_id( '34' ) . '</p>';
Source Link
Morgan Estes
  • 1.5k
  • 12
  • 22

The Codex makes it seem like you can query for a specific comment by ID, as does the GenerateWP query generator, but I couldn't get it to work with either of those examples. Even looking through the WP_Comment_Query:query() code makes it clear that you should be able to pass the ID in the parameters.

That said, using get_comment() is your only way to go for now. Here's what can work based on your original code:

<?php
function wpse120039_get_comment_by_id( $comment_id ) {
    $comment = get_comment( $comment_id );

    return $comment->comment_content;
}

echo '<p>' . wpse120039_get_comment_by_id( '34' ) . '</p>';