Skip to content
wp-skills
Register
tools/wordpress-comment-query-generator

WordPress Comment Query Generator

Build a WP_Comment_Query call with the status and type strings WordPress actually recognises — filter by post, author or parent, count matches, or walk a threaded list.

Restrict to one post. Leave blank to query the whole site.

These are the database's words, not the admin screen's — 'approve', not 'approved'.

'Any type' includes pingbacks and trackbacks alongside real replies.

Threaded returns only top-level comments, with replies attached to each.

Maximum comments to return. Leave blank for all of them.

Comments left by one registered user.

Matches the comment content, author name, email and URL.

Makes query() return a number, so the snippet prints a total rather than a list.

snippet.php
$args = array(
	'status'  => 'approve',
	'type'    => 'comment',
	'orderby' => 'comment_date_gmt',
	'order'   => 'DESC',
	'number'  => 10,
);

$comment_query = new WP_Comment_Query();

$comments = $comment_query->query( $args );

if ( ! empty( $comments ) ) {
	echo '<ul>';

	foreach ( $comments as $comment ) {
		printf(
			'<li><strong>%s</strong>: %s</li>',
			esc_html( $comment->comment_author ),
			esc_html( get_comment_text( $comment ) )
		);
	}

	echo '</ul>';
} else {
	printf( '<p>%s</p>', esc_html__( 'No comments found.', 'text-domain' ) );
}