WP_Comment_Query Generator

Build a WP_Comment_Query or get_comments() call: status, type, post, author, date window, ordering and the fields you need.

Live output

Enable JavaScript to customise; default output below.

Output
Live preview comment-query.php
<?php
/**
 * Comments matching the arguments below.
 */

$args = array(
	// WordPress defaults this to all, which includes spam and trash.
	'status'  => 'approve',
	'type'    => 'comment',
	'orderby' => 'comment_date_gmt',
	'order'   => 'DESC',
	'number'  => 10,
	'update_comment_post_cache' => true,
);

$comments = get_comments( $args );

foreach ( $comments as $comment ) {
	printf(
		'<li><strong>%1$s</strong> %2$s</li>',
		esc_html( $comment->comment_author ),
		esc_html( wp_trim_words( $comment->comment_content, 20 ) )
	);
}

Output is valid and updates as you type.

Build the arguments for get_comments() or WP_Comment_Query, starting from the status WordPress does not default to and the type that separates real comments from pingbacks.

How to use

  1. Set the status. The default is all, which includes spam and trash, and it is the reason a “recent comments” widget sometimes shows spam.
  2. Set the type. comment excludes pingbacks and trackbacks, which are stored in the same table.
  3. Limit the number. Zero means every matching row, and on a busy site the comments table is the largest one after postmeta.
  4. Turn the caches off when you are only reading the comment text. They exist so that reading the parent post is cheap, and cost two queries when you never do.
  5. Use count when you only need the number. It skips building an object per row.

Example

The last ten approved comments, excluding pingbacks:

$args = array(
	// WordPress defaults this to all, which includes spam and trash.
	'status'  => 'approve',
	'type'    => 'comment',
	'orderby' => 'comment_date_gmt',
	'order'   => 'DESC',
	'number'  => 10,
);

$comments = get_comments( $args );

Ordering by comment_date_gmt rather than comment_date avoids a timezone dependent sort, which matters when comments arrive around a DST change.

Pitfalls

  • The default status is all. A widget built without setting it will happily display spam that has not been deleted yet.
  • Pingbacks and trackbacks are comments in the database. type => comment is what excludes them.
  • number => 0 returns everything. The comments table is often the second largest in a WordPress install.
  • search runs a LIKE across author, email, URL, IP and content. On a large table it is slow and unindexed.
  • post_type filters comments by the type of the post they are on, which is how product reviews are separated from blog comments. It joins the posts table to do it.
  • WooCommerce reviews are comments with the type review. A query for comment misses them.
  • count => true returns an integer, not an array. Passing the result to foreach is a fatal.
  • Comment caches are primed by default because most callers then read the post. If you do not, turning them off removes two queries.

Compatibility

get_comments() has been in WordPress since 2.7 and WP_Comment_Query since 3.1, with parent__in and parent__not_in added in 4.4 and the fields => ids shortcut in 4.0. date_query works here exactly as it does in WP_Query, against the comment date columns. The generated code targets PHP 7.0 and up, and the tool runs entirely in your browser.

Frequently asked questions

Why am I seeing spam in my results?
The status defaults to all. Set it to approve.
How do I get WooCommerce reviews?
They are comments with type => review on the product post type. Set both.
Why is my count wrong?
count => true counts rows matching the whole argument set, including the status. A count with status => all includes trash.
How do I get only replies?
parent__not_in => array( 0 ) returns comments that have a parent. parent => 0 returns the top level ones.
Is this cached?
Comment queries use the object cache when one is configured. Without a persistent cache, each request runs the query again.

From the people who built this tool

WP Adminify

The WordPress admin, rebuilt: a dashboard worth looking at, menu and column control, a real file manager and the login page your client sees.

See WP Adminify Free version on WordPress.org

Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.