• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Jetpack a2z

Jetpack a2z

Jetpack a2z

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WPCOM_JSON_API::wp_count_comments() – Counts the number of comments on a site, excluding certain comment types.

You appear to be a bot. Output may be restricted

Description

Counts the number of comments on a site, excluding certain comment types.

Usage

$array = WPCOM_JSON_API::wp_count_comments( $post_id );

Parameters

$post_id
( mixed ) required –

Returns

array Array of counts, matching the output of https://developer.wordpress.org/reference/functions/get_comment_count/.

Source

File name: jetpack/class.json-api.php
Lines:

1 to 73 of 73
  public function wp_count_comments( $post_id ) {
    global $wpdb;
    if ( 0 !== $post_id ) {
      return wp_count_comments( $post_id );
    }

    $counts = array(
      'total_comments' => 0,
      'all'            => 0,
    );

    
/**
 * Exclude certain comment types from comment counts in the REST API.
 *
 * @since 6.9.0
 * @module json-api
 *
 * @param array Array of comment types to exclude (default: 'order_note', 'webhook_delivery', 'review', 'action_log')
 */
    $exclude = apply_filters(
      'jetpack_api_exclude_comment_types_count',
      array( 'order_note', 'webhook_delivery', 'review', 'action_log' )
    );

    if ( empty( $exclude ) ) {
      return wp_count_comments( $post_id );
    }

    array_walk( $exclude, 'esc_sql' );
    $where = sprintf(
      "WHERE comment_type NOT IN ( '%s' )",
      implode( "','", $exclude )
    );

    $count = $wpdb->get_results(
      "SELECT comment_approved, COUNT(*) AS num_comments
				FROM $wpdb->comments
				{$where}
				GROUP BY comment_approved
			"
    );

    $approved = array(
      '0'            => 'moderated',
      '1'            => 'approved',
      'spam'         => 'spam',
      'trash'        => 'trash',
      'post-trashed' => 'post-trashed',
    );

    // https://developer.wordpress.org/reference/functions/get_comment_count/#source
    foreach ( $count as $row ) {
      if ( ! in_array( $row->comment_approved, array( 'post-trashed', 'trash', 'spam' ), true ) ) {
        $counts['all']            += $row->num_comments;
        $counts['total_comments'] += $row->num_comments;
      } elseif ( ! in_array( $row->comment_approved, array( 'post-trashed', 'trash' ), true ) ) {
        $counts['total_comments'] += $row->num_comments;
      }
      if ( isset( $approved[ $row->comment_approved ] ) ) {
        $counts[ $approved[ $row->comment_approved ] ] = $row->num_comments;
      }
    }

    foreach ( $approved as $key ) {
      if ( empty( $counts[ $key ] ) ) {
        $counts[ $key ] = 0;
      }
    }

    $counts = (object) $counts;

    return $counts;
  }
 

 View on GitHub View on Trac

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Related

Published: 27 November 2019 | Last updated: 27 November 2019

Primary Sidebar

Information

Function name: WPCOM_JSON_API::wp_count_comments
Class ref: WPCOM_JSON_API
Plugin ref: Jetpack
Version: 9.2
Sourcefile: class.json-api.php
File ref: class.json-api.php
Deprecated?: No
API Letters: A,C,J,W

Footer

Jetpack a2z
Jetpack a2z
Jetpack a2z
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  jetpack.wp-a2z.org
© Copyright Jetpack a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites