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.
<?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.
Fix the highlighted fields to update the output.
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
- Set the status. The default is
all, which includes spam and trash, and it is the reason a “recent comments” widget sometimes shows spam. - Set the type.
commentexcludes pingbacks and trackbacks, which are stored in the same table. - Limit the number. Zero means every matching row, and on a busy site the comments table is the largest one after postmeta.
- 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.
- Use
countwhen 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 => commentis what excludes them. number => 0returns everything. The comments table is often the second largest in a WordPress install.searchruns aLIKEacross author, email, URL, IP and content. On a large table it is slow and unindexed.post_typefilters 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 forcommentmisses them. count => truereturns an integer, not an array. Passing the result toforeachis 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?
all. Set it to approve.How do I get WooCommerce reviews?
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.