Function and usage of get_comment_type() in wordpress


Answers ( 1 )


    The get_comment_type() function in WordPress is used to retrieve the type of a specific comment. This function is particularly useful when dealing with different kinds of comments on a post, such as standard comments, pingbacks, or trackbacks.

    Here's a detailed explanation of its usage:

    Function Signature

    get_comment_type( int|WP_Comment $comment_id = null ): string


    • $comment_id (int|WP_Comment) (optional): This parameter can either be the integer ID of the comment or an instance of the WP_Comment object. If this parameter is not provided, the function will automatically use the current comment in the WordPress loop.

    Return Value

    • Return Type: string
    • Description: The function returns the type of the comment. The return value is a string which can be 'comment' (for a regular comment), 'pingback', 'trackback', or any custom comment type registered by plugins or themes.


    Example 1: Get Comment Type of the Current Comment

    // Within the loop
    $comment_type = get_comment_type();
    echo 'Comment type: ' . $comment_type;

    This example is typically used within the WordPress loop. If no parameter is passed to get_comment_type(), it defaults to the current comment in the loop.

    Example 2: Get Comment Type by Comment ID

    // Assuming you have a comment ID
    $comment_id = 123;
    $comment_type = get_comment_type($comment_id);
    echo 'Comment type: ' . $comment_type;

    In this example, the function is used to get the type of a comment with a specific ID.

    Example 3: Using a WP_Comment Object

    // Assuming you have a WP_Comment object
    $comment = get_comment(123);
    $comment_type = get_comment_type($comment);
    echo 'Comment type: ' . $comment_type;

    Here, the function is used with a WP_Comment object. This is useful when you have the comment object available and want to determine its type.


    The get_comment_type() function is a straightforward and efficient way to determine the type of a comment in WordPress. It's useful for theme and plugin developers who need to handle different types of comments differently, such as displaying them in a unique way or applying specific filters based on their type.

Leave an answer