comments_clauses │ filter-hook │ WC 3.1.0

This is a WordPress - comments_clauses hook. The plugin just uses it.

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

Where the hook is called

WC_REST_Report_Reviews_Totals_Controller::get_reports()
comments_clauses
woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-report-reviews-totals-controller.php 96
$clauses = apply_filters_ref_array( 'comments_clauses', array( $clauses, &$comment_query ) );

Where the hook is used in WooCommerce

woocommerce/includes/class-wc-comments.php 48
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ) );
woocommerce/includes/class-wc-comments.php 53
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
woocommerce/includes/class-wc-comments.php 57
add_filter( 'comments_clauses', array( __CLASS__, 'exclude_action_log_comments' ), 10, 2 );
woocommerce/includes/class-wc-comments.php 61
add_filter( 'comments_clauses', array( ReviewsUtil::class, 'comments_clauses_without_product_reviews' ), 10, 2 );
woocommerce/includes/class-wc-order.php 2177
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
woocommerce/includes/class-wc-order.php 2189
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-order-notes-v1-controller.php 196
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-order-notes-v1-controller.php 200
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-order-notes-v2-controller.php 66
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-order-notes-v2-controller.php 70
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/includes/wc-order-functions.php 1308
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/includes/wc-order-functions.php 1312
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/packages/action-scheduler/classes/data-stores/ActionScheduler_wpCommentLogger.php 129
add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 );
woocommerce/src/Internal/RestApi/Routes/V4/OrderNotes/CollectionQuery.php 85
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/src/Internal/RestApi/Routes/V4/OrderNotes/CollectionQuery.php 87
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
woocommerce/src/StoreApi/Routes/V1/ProductReviews.php 127
add_filter( 'comments_clauses', $exclude_password_protected_reviews );
woocommerce/src/StoreApi/Routes/V1/ProductReviews.php 146
remove_filter( 'comments_clauses', $exclude_password_protected_reviews );