Function and usage of get_comment_meta() in wordpress


Answers ( 1 )


    The get_comment_meta() function in WordPress is a useful tool for retrieving metadata associated with comments in your WordPress site. Here's a detailed explanation of the function and its usage:


    get_comment_meta( int $comment_id, string $key = '', bool $single = false ): mixed

    This function is used to retrieve metadata for a specific comment. Metadata is additional information stored about comments, such as custom fields added by plugins or themes.


    1. $comment_id (int, required): This is the ID of the comment for which you want to retrieve metadata. It is a unique identifier for each comment in WordPress.

    2. $key (string, optional): This parameter specifies the specific meta key for which you want to retrieve the value. If left empty (''), the function will return all metadata associated with the specified comment.

      • Default value: ''
    3. $single (bool, optional): This parameter determines how the function returns the value of the meta field.

      • If set to true, it will return a single value (the first one found).

      • If set to false, it will return an array of values associated with the meta key.

      • This parameter is only effective if $key is specified.

      • Default value: false

    Return Values:

    • Mixed: The type of the returned value depends on the input parameters:
      • An array of values if $single is false.
      • The value of the meta data field if $single is true.
      • false if $comment_id is invalid (non-numeric, zero, or negative).
      • An empty string if a valid, but non-existing, comment ID is passed.

    Sample Usage:

    1. Retrieve All Metadata for a Comment:

    $comment_id = 123; // Replace with your comment ID
    $all_meta = get_comment_meta($comment_id);

    This will retrieve all metadata for the comment with ID 123.

    2. Retrieve Specific Meta Field for a Comment:

    $comment_id = 123; // Replace with your comment ID
    $key = 'custom_field'; // Replace with your meta key
    $single = true;
    $value = get_comment_meta($comment_id, $key, $single);

    This will retrieve the value of 'custom_field' for the comment with ID 123.

    3. Retrieve Multiple Values for a Meta Key:

    $comment_id = 123; // Replace with your comment ID
    $key = 'custom_field'; // Replace with your meta key
    $values = get_comment_meta($comment_id, $key);

    If 'custom_field' has multiple values, this will return them as an array.


    The get_comment_meta() function is a flexible and essential tool for accessing comment-related metadata in WordPress, useful for developers working with custom data or extending the functionality of WordPress comments.

Leave an answer