LLMS_Comments::wp_count_comments( stdClass|array $stats, int $post_id )

Remove order notes from the count when counting comments

Description Description

This method is hooked to wp_count_comments, called by wp_count_comments().

It handles two potential scenarios:

1) No other plugins have run the filter and the incoming $stats is an empty array. In this scenario we’ll utilize get_comment_count() to create a new $stats object

2) Another plugin has already generated a stats object and then incoming $stats is a stdClass.

In either scenario we query the number of order notes and subtract this number from the existing comment counts.

Top ↑

Parameters Parameters


(stdClass|array) (Required) Empty array or a stdClass of stats from another plugin.


(int) (Required) WP Post ID. 0 indicates comment stats for the entire site.

Top ↑

Return Return

(stdClass) The number of comments keyed by their status.

  • 'approved'
    (int) The number of approved comments.
  • 'moderated'
    (int) The number of comments awaiting moderation (a.k.a. pending).
  • 'spam'
    (int) The number of spam comments.
  • 'trash'
    (int) The number of trashed comments.
  • 'post-trashed'
    (int) The number of comments for posts that are in the trash.
  • 'total_comments'
    (int) The total number of non-trashed comments, including spam.
  • 'all'
    (int) The total number of pending or approved comments.

Top ↑

Source Source

File: includes/class.llms.comments.php

	public static function wp_count_comments( $stats, $post_id ) {

		// If someone calls this directly on 6.0 or later notify them and return early.
		if ( ! self::should_modify_comment_counts() ) {
			_doing_it_wrong( __METHOD__, 'This method should not be called on WordPress 6.0 or later.', '6.6.0' );
			return $stats;

		// Don't modify when querying for a specific post.
		if ( 0 !== $post_id ) {
			return $stats;

		// Return cached object if available.
		$cached = get_transient( self::$count_transient_key );
		if ( $cached ) {
			return $cached;

		// If $stats is empty, get a new object from the WP Core that we can modify.
		if ( empty( $stats ) ) {

			$stats = get_comment_count( $post_id );

			// The keys in wp_count_comments() and get_comment_counts() don't match.
			$stats['moderated'] = $stats['awaiting_moderation'];
			unset( $stats['awaiting_moderation'] );

			// Cast to an object.
			$stats = (object) $stats;


		// Otherwise modify the existing stats object.
		return self::modify_comment_stats( $stats );


Top ↑

Changelog Changelog

Version Description
6.6.0 Will throw _doing_it_wrong() when run on WP 6.0 or later and return the input $stats unchanged.
3.37.12 Use strict comparisons. Fix issue encountered when $stats is an empty array. Modify the stats generation method.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.