comments_clauses
Allows you to change SQL clauses (fields, join, where, orderby, limits, groupby) when comments are retrieved by get_comments().
Usage
add_filter( 'comments_clauses', 'wp_kama_comments_clauses_filter' );
/**
* Function for `comments_clauses` filter-hook.
*
* @param string[] $clauses Associative array of the clauses for the query.
*
* @return string[]
*/
function wp_kama_comments_clauses_filter( $clauses ){
// filter...
return $clauses;
}
- $pieces(string[])
- An associative array containing SQL clauses such as fields, join, where, orderby, limits, groupby, and so on.
- $query(WP_Comment_Query)
- The current WP_Comment_Query object. PHP objects are passed by reference.
Examples
#1 Example filter data
In the admin dashboard's “At a Glance” meta box, wp_dashboard_site_activity() calls wp_dashboard_recent_comments(), which then uses get_comments() to display recent comments. For an administrator, the query code looks like this:
$comments_query = array( 'number' => 25, 'offset' => 0, ); get_comments( $comments_query )
Use WordPress debugging to see which data passes through the filter:
add_filter( 'comments_clauses', function ( $args ) {
error_log( print_r( $args, true ) );
return $args;
} );
The result:
Array ( [fields] => wp_comments.comment_ID [join] => [where] => ( ( comment_approved = '0' OR comment_approved = '1' ) ) [orderby] => wp_comments.comment_date_gmt DESC [limits] => LIMIT 0,25 [groupby] => )
#2 wpDiscuz: sort by popular comments based on WP Recall data
Changelog
| Since 3.1.0 | Introduced. |
Where the hook is called
comments_clauses
wp-includes/class-wp-comment-query.php 958
$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );