Class: WP_Comment

Core class used to organize comments as instantiated objects with defined members.


Properties

Name Type(s) Default Value Summary
comment_ID integer Comment ID.
comment_post_ID integer ID of the post the comment is associated with.
comment_author string Comment author name.
comment_author_email string Comment author email address.
comment_author_url string Comment author URL.
comment_author_IP string Comment author IP address (IPv4 format).
comment_date string Comment date in YYYY-MM-DD HH:MM:SS format.
comment_date_gmt string Comment GMT date in YYYY-MM-DD HH::MM:SS format.
comment_content string Comment content.
comment_karma integer Comment karma count.
comment_approved string Comment approval status.
comment_agent string Comment author HTTP user agent.
comment_type string Comment type.
comment_parent integer Parent comment ID.
user_id integer Comment author ID.
children array Comment children.
populated_children boolean Whether children have been populated for this comment object.
post_fields array Post fields.

Methods

WP_Comment:: __construct( WP_Comment $comment )

Constructor. Since 4.4.0.

Populates properties with object vars.

Arguments

Name Type(s) Default Value Description
$comment WP_Comment

Comment object.


WP_Comment:: __get( string $name )

Magic getter. Since 4.4.0.

If $name matches a post field, the comment post will be loaded and the post's value returned.

Arguments

Name Type(s) Default Value Description
$name string

Returns

mixed


WP_Comment:: __isset( string $name )

Check whether a non-public property is set. Since 4.4.0.

If $name matches a post field, the comment post will be loaded and the post's value checked.

Arguments

Name Type(s) Default Value Description
$name string

Property name.

Returns

boolean


WP_Comment:: add_child( WP_Comment $child )

Add a child to the comment. Since 4.4.0.

Used by WP_Comment_Query when bulk-filling descendants.

Arguments

Name Type(s) Default Value Description
$child WP_Comment

Child comment.


WP_Comment:: get_child( integer $child_id )

Get a child comment by ID. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$child_id integer

ID of the child.

Returns

WP_Comment | boolean

Returns the comment object if found, otherwise false.


WP_Comment:: get_children( array $args = array() )

Get the children of a comment. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$args array

{ Array of arguments used to pass to get_comments() and determine format.

@type string $format        Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
                            Default 'tree'.
@type string $status        Comment status to limit results by. Accepts 'hold' (`comment_status=0`),
                            'approve' (`comment_status=1`), 'all', or a custom comment status.
                            Default 'all'.
@type string $hierarchical  Whether to include comment descendants in the results.
                            'threaded' returns a tree, with each comment's children
                            stored in a `children` property on the `WP_Comment` object.
                            'flat' returns a flat array of found comments plus their children.
                            Pass `false` to leave out descendants.
                            The parameter is ignored (forced to `false`) when `$fields` is 'ids' or 'counts'.
                            Accepts 'threaded', 'flat', or false. Default: 'threaded'.
@type string|array $orderby Comment status or array of statuses. To use 'meta_value'
                            or 'meta_value_num', `$meta_key` must also be defined.
                            To sort by a specific `$meta_query` clause, use that
                            clause's array key. Accepts 'comment_agent',
                            'comment_approved', 'comment_author',
                            'comment_author_email', 'comment_author_IP',
                            'comment_author_url', 'comment_content', 'comment_date',
                            'comment_date_gmt', 'comment_ID', 'comment_karma',
                            'comment_parent', 'comment_post_ID', 'comment_type',
                            'user_id', 'comment__in', 'meta_value', 'meta_value_num',
                            the value of $meta_key, and the array keys of
                            `$meta_query`. Also accepts false, an empty array, or
                            'none' to disable `ORDER BY` clause.

}

Returns

array

Array of WP_Comment objects.


WP_Comment:: get_instance( integer $id )

Retrieves a WP_Comment instance. Since 4.4.0.

Arguments

Name Type(s) Default Value Description
$id integer

Comment ID.

Returns

WP_Comment | false

Comment object, otherwise false.


WP_Comment:: populated_children( boolean $set )

Set the 'populated_children' flag. Since 4.4.0.

This flag is important for ensuring that calling get_children() on a childless comment will not trigger unneeded database queries.

Arguments

Name Type(s) Default Value Description
$set boolean

Whether the comment's children have already been populated.


WP_Comment:: to_array( )

Convert object to array. Since 4.4.0.

Returns

array

Object as array.


WordPress Developer Newsletter

Stay informed of new chapter releases, important WordPress API updates and more.